#include"LinkList.h"pLinkNode BuyNode(DataType x)//新增节点开辟空间
{
pLinkNode newNode= (pLinkNode)malloc(sizeof(LinkNode));if (newNode ==NULL)
{
printf("out of memory\n");
exit(EXIT_FAILURE);
}
newNode->data =x;
newNode->next =NULL;returnnewNode;
}void Init(pLinkList pList)//初始化链表
{
assert(pList);
pList->phead =NULL;
}void PrintList(pLinkList pList)//打印链表
{
assert(pList);
pLinkNode cur= pList->phead;while (cur !=NULL)
{
printf("%d->", cur->data);
cur= cur->next;
}
printf("NULL\n");
}void DestroyList(pLinkList pList)//销毁链表
{
assert(pList);
pLinkNode cur= pList->phead;
pLinkNode del=NULL;while (cur !=NULL)
{
del=cur;
cur= cur->next;free(del);
}
pList->phead =NULL;
}void PushFront(pLinkList pList, DataType x)//头插
{
assert(pList);
pLinkNode newNode=BuyNode(x);
pLinkNode cur= pList->phead;
newNode->next =cur;
pList->phead =newNode;
}void PushBack(pLinkList pList, DataType x) //尾插
{
assert(pList);
pLinkNode newNode=BuyNode(x);
pLinkNode cur= pList->phead;if (cur ==NULL)
{
pList->phead =newNode;return;
}
pLinkNode prev=NULL;while (cur!=NULL)
{
prev=cur;
cur= cur->next;
}
prev->next=newNode;
}void PopFront(pLinkList pList) //头删
{
pLinkNode cur= pList->phead;if (cur ==NULL)
{
printf("该链表无元素可删\n");
}else{
pList->phead = cur->next;free(cur);
cur=NULL;
}
}void PopBack(pLinkList pList)//尾删
{
pLinkNode cur= pList->phead;
pLinkNode prev=NULL;if (cur ==NULL)
{
printf("该链表无元素可删\n");
}else if (cur->next == NULL) //链表中只有一个元素
{
pList->phead =NULL;free(cur);
}else{while (cur->next !=NULL)
{
prev=cur;
cur= cur->next;
}free(cur);
prev->next =NULL;
}
}void Insert(pLinkList pList, pLinkNode Pos, DataType x)//指定位置之前插入
{
assert(pList);
pLinkNode cur= pList->phead;
pLinkNode newNode=BuyNode(x);if (Pos ==cur)
{
PushFront(pList, x);
}else if (Pos ==NULL)
{
printf("输入位置错误\n");return;
}else{while(cur)
{if (cur->next ==Pos)
{
cur->next =newNode;
newNode->next =Pos;
}
cur= cur->next;
}
}
}
pLinkNode Find(pLinkList pList, DataType x)//公用的查找函数
{
assert(pList);
pLinkNode cur= pList->phead;while (cur!=NULL)
{if (cur->data ==x)
{returncur;
}
cur= cur->next;
}returnNULL;
}void Search(pLinkList pList, DataType x)//查找指定元素
{
assert(pList);
pLinkNode cur=NULL;
cur=Find(pList, x);if (cur !=NULL)
{
printf("该元素存在");
}
printf("该元素不存在\n");
}void Remove(pLinkList pList, DataType x) //删除指定元素
{
assert(pList);
pLinkNode del=NULL;
pLinkNode cur=NULL;
DataType tmp;
cur=Find(pList, x);if (cur == pList->phead) //链表只有一个元素时
{
PopFront(pList);
}else if (cur->next == NULL) //cur现在指向最后一个元素
{
PopBack(pList);
}else if (cur !=NULL)
{
tmp= cur->data;
cur->data = cur->next->data;
cur->next->data =tmp;
del= cur->next;
cur->next = cur->next->next;free(del);
}else{
printf("该元素不存在\n");
}
}void RemoveALL(pLinkList pList, DataType x) //删除所有出现的元素
{
assert(pList);
pLinkNode del=NULL;
pLinkNode cur= pList->phead;
DataType tmp;while (cur !=NULL)
{
cur=Find(pList, x);if (cur ==NULL)
{return;
}if (cur == pList->phead) //链表只有一个元素时
{
PopFront(pList);
}else if (cur->next == NULL) //cur现在指向最后一个元素
{
PopBack(pList);
}else{
tmp= cur->data;
cur->data = cur->next->data;
cur->next->data =tmp;
del= cur->next;
cur->next = cur->next->next;free(del);
}
}
}void Erase(pLinkList pList, pLinkNode pos)//删除指定位置的元素
{
assert(pList);
pLinkNode prev=NULL;
pLinkNode del=NULL;
pLinkNode cur= pList->phead;if (pos ==NULL)
{
printf("该位置不存在\n");
}else if (cur ==pos)
{
PopFront(pList);
}else{while (cur !=pos)
{
prev=cur;
cur= cur->next;
}
del=cur;
prev->next = cur->next;free(del);
}
}void BubbleSort(pLinkList pList)//对链表进行冒泡排序
{
assert(pList);
pLinkNode cur= pList->phead;
pLinkNode j=NULL;for (; cur != NULL; cur = cur->next)
{for (j = cur->next; j != NULL; j = j->next)
{if (cur->data>j->data)
{
DataType tmp= cur->data;
cur->data = j->data;
j->data =tmp;
}
}
}
}