本文只要实现单链表的初始化、插入(尾插、头插、任意位置插入)、删除(尾删、头删、删除指定元素)、查找等。
定义单链表
typedef int DataType;
typedef struct LinkNode
{
DataType data;
struct LinkNode *next;
}LinkNode, *pLinkNode, *pList;
实现单链表的所有接口:
void InitLinkList(pList* pHead);//单链表的初始化
void Destroy(pList *pHead);//销毁(动态开辟的内存释放)
void PushBack(pList* pHead, DataType x);//尾插
void PopBack(pList* pHead);//尾删
void PushFront(pList* pHead, DataType x);//头插
void PopFront(pList* pHead);//头删
void PrintList(pList list);//打印单链表的元素
int GetListLength(pList head);//获取单链表的长度
pLinkNode Find(pList head, DataType x);//查找元素
void Insert(pList *pHead, pLinkNode pos, DataType x);//任意位置插入元素
void Remove(pList *pHead, DataType x);//删除指定元素
void RemoveAll(pList *pHead, DataType x);//删除单链表中所有指定元素
void Erase(pList *pHead, pLinkNode pos);//按位置删除
实现接口的函数:
void InitLinkList(pList* pHead)//单链表的初始化
{
assert(pHead);
*pHead = NULL;
}
void Destroy(pList *pHead)//销毁(动态开辟的内存释放)
{
assert(pHead);
pLinkNode del = NULL;
pLinkNode cur = *pHead;
while (*pHead)
{
del = *pHead;
*pHead = (*pHead)->next;
free(del);
del = NULL;
}
}
pLinkNode BuyNode(DataType x)//创建新节点
{
pLinkNode NewNode = (pLinkNode)malloc(sizeof(LinkNode));
NewNode->data = x;
NewNode->next = NULL;
return NewNode;
}
void PushBack(pList* pHead, DataType x)
{
assert(pHead);
pLinkNode cur = *pHead;
pLinkNode NewNode=BuyNode(x);
if (cur == NULL)
{
*pHead = NewNode;
}
else
{
while (cur->next)
{
cur = cur->next;
}
cur->next = NewNode;
}
}
void PrintList(pList list)
{
pLinkNode cur = list;
printf("list is: ");
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("over\n");
}
void PopBack(pList* pHead)
{
assert(pHead);
pLinkNode cur = *pHead;
pLinkNode del = NULL;
if (cur == NULL)
{
return;
}
else if (cur->next == NULL)
{
free(cur);
*pHead = NULL;
}
else
{
while (cur->next->next)
{
cur = cur->next;
}
del = cur->next;
cur->next = NULL;
free(del);
del = NULL;
}
}
void PushFront(pList* pHead, DataType x)
{
assert(pHead);
pLinkNode NewNode = BuyNode(x);
pLinkNode cur = *pHead;
if (cur == NULL)
{
*pHead = NewNode;
}
else
{
NewNode->next = *pHead;
*pHead = NewNode;
}
}
void PopFront(pList* pHead)
{
assert(pHead);
pLinkNode cur = *pHead;
pLinkNode del = cur;
if (cur == NULL)
{
return;
}
else
{
*pHead = cur->next;
free(del);
del = NULL;
}
}
int GetListLength(pList head)
{
int count = 0;
pLinkNode cur = head;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
pLinkNode Find(pList head, DataType x)
{
pLinkNode cur = head;
while (cur)
{
if (cur->data == x)
return cur;
else
cur = cur->next;
}
return NULL;
}
void Insert(pList *pHead, pLinkNode pos, DataType x)
{
assert(pHead);
assert(pos);
pLinkNode cur = *pHead;
pLinkNode NewNode = BuyNode(x);
if (cur ==pos)
{
NewNode->next = cur;
*pHead = NewNode;
}
else
{
while (cur->next!=pos)
{
cur = cur->next;
}
NewNode->next =cur->next;
cur->next = NewNode;
}
}
void Remove(pList *pHead, DataType x)
{
assert(pHead);
pLinkNode cur = *pHead;
pLinkNode prev = NULL;
pLinkNode del = NULL;
while (cur)
{
if (cur->data == x)
{
del = cur;
if (cur == *pHead)
{
*pHead = cur->next;
}
else
{
prev->next = cur->next;
}
free(del);
del = NULL;
break;
}
prev = cur;
cur = cur->next;
}
}
void Erase(pList *pHead, pLinkNode pos)
{
assert(pHead);
assert(pos);
pLinkNode cur = *pHead;
pLinkNode del = cur;
pLinkNode prev = NULL;
while (cur)
{
del = cur;
if (cur == pos)
{
if (cur == *pHead)
{
*pHead = cur->next;
}
else
{
prev->next = cur->next;
}
free(del);
del = NULL;
break;
}
prev = cur;
cur = cur->next;
}
}
void RemoveAll(pList *pHead, DataType x)
{
assert(pHead);
pLinkNode cur = *pHead;
pLinkNode prev = NULL;
pLinkNode del = NULL;
while (cur)
{
if (cur->data == x)
{
del = cur;
if (cur == *pHead)
{
*pHead = cur->next;
cur = *pHead;
}
else
{
prev->next = cur->next;
cur = prev->next;
}
free(del);
del = NULL;
}
else
{
prev = cur;
cur = cur->next;
}
}
}