链表 mysql 模拟_模拟实现链表

#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;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值