c语言实现单链表
list.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __LIST_H__
#define __LIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node* next;
}Node, *pNode, *pList;
void InitList(pList* pplist);
void PushBack(pList* pplist, DataType d);
void PopBack(pList* pplist);
void PushFront(pList* pplist, DataType d);
void PopFront(pList* pplist);
void PrintList(pList plist);
pNode Find(pList plist, DataType d);
void Remove(pList* pplist, DataType d);
void RemoveAll(pList* pplist, DataType d);
void Insert(pList* pplist,pNode pos, DataType d);
void Erase(pList* pplist, pNode pos);
void ReverseList(pList* pplist);
void BubbleSort(pList* pplist, pList plist);
int get_length(pList* pplist);
void DestroyList(pList* pplist);
#endif
list.c
}
pNode BuyNode(DataType d)
{
pNode newnode = (pNode)malloc(sizeof(Node));
newnode->data = d;
newnode->next = NULL;
return newnode;
}
void PushBack(pList* pplist, DataType d)
{
pNode newnode = BuyNode(d);
pNode cur = *pplist;
assert(pplist);
if (*pplist = NULL)
{
*pplist = newnode;
return;
}
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newnode;
}
void PopBack(pList* pplist)
{
pNode cur = *pplist;
assert(pplist);
if (*pplist == NULL)
return;
if (((*pplist)->next) == NULL)
{
free(*pplist);
*pplist = NULL;
return;
}
while ((cur->next->next) != NULL)
{
cur = cur->next;
}
free(cur->next);
cur->next = NULL;
}
void PushFront(pList* pplist, DataType d)
{
pNode cur = *pplist;
pNode newnode = BuyNode(d);
assert(pplist);
if (cur == NULL)
{
*pplist = newnode;
}
else
{
newnode->next = cur;
*pplist = newnode;
}
}
void PopFront(pList* pplist)
{
pNode cur = *pplist;
assert(pplist);
if (*pplist == NULL)
return;
if (((*pplist)->next) == NULL)
{
free(*pplist);
*pplist = NULL;
return;
}
else
{
*pplist = cur->next;
free(cur);
}
}
void DestroyList(pList* pplist)
{
pNode cur = *pplist;
while (cur)
{
pNode del = cur;
cur = cur->next;
free(del);
del = NULL;
}
*pplist = NULL;
}
void PrintList(pList plist)
{
pNode cur = plist;
while (cur)
{
printf("%d--->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
pNode Find(pList plist, DataType d)
{
pNode cur = plist;
while (cur)
{
if (cur->data == d)
return cur;
cur = cur->next;
}
return NULL;
}
void Remove(pList* pplist, DataType d)
{
pNode cur = *pplist;
pNode prev = *pplist;
assert(pplist);
if (*pplist == NULL)
return;
while (cur)
{
if (cur->data == d)
{
pNode del = NULL;
if (cur == *pplist)
{
*pplist = cur->next;
}
else
{
prev->next = cur->next;
}
free(del);
del = NULL;
return;
}
else
{
prev = cur;
cur = cur->next;
}
}
}
void RemoveAll(pList* pplist, DataType d)
{
pNode cur = *pplist;
pNode prev = *pplist;
assert(pplist);
if (*pplist == NULL)
return;
while (cur)
{
if (cur->data == d)
{
pNode del = NULL;
if (cur == *pplist)
{
*pplist = cur->next;
}
else
{
prev->next = cur->next;
}
cur = cur->next;
free(del);
del = NULL;
return;
}
else
{
prev = cur;
cur = cur->next;
}
}
}
void Insert(pList* pplist, pNode pos, DataType d)
{
pNode cur = *pplist;
pNode newnode = BuyNode(d);
assert(pplist);
newnode->next = pos->next;
pos->next = newnode;
}
void Erase(pList* pplist, pNode pos)
{
assert(pplist);
pNode cur = *pplist;
pNode del = NULL;
pNode prev = NULL;
if (cur->next == NULL)
{
if (pos == cur)
{
free(cur);
*pplist= NULL;
}
return;
}
if (pos == cur)
{
del = cur;
cur = cur->next;
free(del);
return;
}
cur = cur->next;
while (cur)
{
if (cur == pos)
{
prev->next = cur->next;
free(cur);
return;
}
prev = cur;
cur = cur->next;
}
}
void ReverseList(pList* pplist)
{
pNode cur = *pplist;
pNode p = cur->next;
if (cur!=NULL)
{
pNode q = p->next;
cur->next = NULL;
while (cur)
{
p->next = cur->next;
cur->next = p;
if (q)
{
p = q;
q = p->next;
}
else
break;
}
}
return cur;
}
int get_length(pList* pplist)
{
int count = 0;
pNode cur = *pplist;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
void BubbleSort(pList* pplist, pList plist)
{
int i = 0;
int j = 0;
pNode tmp = NULL;
int count = 0;
count = get_length(&plist);
for (i = 0; i < count - 1; i++)
{
pNode cur = *pplist;
for (j = 0; j<count - i - 1; j++)
{
if (cur->data > cur->next->data)
{
tmp = cur->data;
cur->data = cur->next->data;
cur->next->data = tmp;
}
cur = cur->next;
}
}
}