c语言中链表的实现
来源:互联网 发布:行车记录仪支架淘宝 编辑:程序博客网 时间:2024/05/17 17:38
linklist.h
[cpp] view plain copy
- <span style="font-size:18px;">#ifndef __LINKLIST_H__
- #define __LINKLIST_H__
- #include<stdio.h>
- #include<stdlib.h>
- #include<assert.h>
- typedef int DataType;
- typedef struct LinkNode
- {
- DataType data;
- struct LinkNode* next;
- }LinkNode,*pLinkNode;
- typedef struct LinkList
- {
- LinkNode* pHead;
- }LinkList,*pLinkList;
- void InitLinkList(pLinkList list);
- void DestoryList(pLinkList list);
- void PushBack(pLinkList list , DataType x);
- void PopBack(pLinkList list);
- void PushFront(pLinkList list , DataType x);
- void PopFront(pLinkList list);
- void PrintList(pLinkList list);
- pLinkNode Find(pLinkList list,DataType x);
- void Insert(pLinkList list, pLinkNode pos, DataType x);
- void Remove(pLinkList list, DataType x);
- void RemoveAll(pLinkList list, DataType x);
- void Erase(pLinkList list,pLinkNode pos);
- void BubbleSort(pLinkList list);
- void SelectSort(pLinkList list);
- void InsertSort(pLinkList list);
- #endif //__LINKLIST_H__</span>
[cpp] view plain copy
- <strong><span style="font-size:18px;">#include"linklist.h"
- void CreateNode(pLinkNode *newNode, DataType x) //创建节点
- {
- *newNode = (pLinkNode)malloc(sizeof(LinkNode));
- if (NULL == *newNode)
- {
- printf("out of memory\n");
- exit(EXIT_FAILURE);
- }
- (*newNode)->data = x;
- (*newNode)->next = NULL;
- }
- void InitLinkList(pLinkList list) //初始化
- {
- list->pHead = NULL;
- assert(list);
- }
- void DestoryList(pLinkList list)
- {
- assert(list);
- if (NULL==list->pHead) //链表为空直接返回
- {
- return;
- }
- else
- {
- pLinkNode cur = list->pHead; //cur指向第一个结点
- while (cur != NULL)
- {
- list->pHead = cur->next; //pHead指向cur的下一个结点,当cur是最后一个结点时,pHead指向空
- free(cur);
- cur = list->pHead; //cur指向当前第一个结点,当链表为空时,cur指向空
- }
- }
- }
- void PushBack(pLinkList list, DataType x)
- {
- pLinkNode newNode = NULL;
- assert(list);
- CreateNode(&newNode,x);
- if (NULL == (list->pHead)) //如果是空链表,直接插入头指针之后
- {
- list->pHead = newNode;
- }
- else
- {
- pLinkNode cur = list->pHead;
- while (NULL != (cur->next)) //找到最后一个结点cur
- {
- cur = cur->next;
- }
- cur->next = newNode;
- }
- }
- void PopBack(pLinkList list)
- {
- assert(list);
- if (NULL == list->pHead)
- {
- return;
- }
- else
- {
- pLinkNode cur = list->pHead;
- if (NULL == cur->next) //如果只有一个结点
- {
- free(cur);
- list->pHead= NULL;
- }
- else
- {
- while (NULL != cur->next->next) //大于一个结点,先找到倒数第二个结点
- {
- cur = cur->next;
- }
- free(cur->next);
- cur->next= NULL;
- }
- }
- }
- void PushFront(pLinkList list, DataType x)
- {
- pLinkNode newNode = NULL;
- assert(list);
- CreateNode(&newNode, x);
- newNode->next =list->pHead; //newNode的指针域先指向第一个结点
- list->pHead= newNode; //头指针指向newNode
- }
- void PopFront(pLinkList list)
- {
- pLinkNode cur = list->pHead; //cur指向第一个结点
- assert(list);
- if (NULL == list->pHead) //空链表
- {
- return;
- }
- list->pHead = cur->next; //指向第一个结点的指针域
- free(cur);
- cur = NULL;
- }
- void PrintList(pLinkList list)
- {
- pLinkNode cur = list->pHead;
- assert(list);
- while (NULL != cur)
- {
- printf("%d->", cur->data);
- cur = cur->next;
- }
- printf("over\n");
- }
- pLinkNode Find(pLinkList list, DataType x)
- {
- pLinkNode cur = list->pHead;
- assert(list);
- while (NULL != cur)
- {
- if (cur->data == x)
- {
- break;
- }
- cur = cur->next;
- }
- return cur;
- }
- void Insert(pLinkList list, pLinkNode pos, DataType x) //在pos后面插入元素
- {
- pLinkNode cur = list->pHead;
- pLinkNode newNode = NULL;
- assert(list);
- CreateNode(&newNode, x);
- while (NULL != cur) //先找到这个位置
- {
- if (cur == pos)
- {
- break;
- }
- cur = cur->next;
- }
- if (NULL != cur)
- {
- newNode->next=cur->next;
- cur->next = newNode;
- }
- else
- {
- printf("没有这个结点\n");
- }
- }
- void Remove(pLinkList list, DataType x)
- {
- pLinkNode cur = list->pHead;
- pLinkNode p = list->pHead;
- assert(list);
- if (NULL == list->pHead) //空链表直接返回
- {
- return;
- }
- if (NULL == cur->next) //如果只有一个结点
- {
- if (cur->data == x)
- {
- list->pHead = cur->next;
- free(cur);
- return;
- }
- }
- else
- {
- if (cur->data == x) //先判断第一个结点是不是要删除的结点
- {
- list->pHead = cur->next;
- free(cur);
- return;
- }
- cur = cur->next;
- while (NULL != cur)
- {
- if (cur->data == x)
- {
- p->next = cur->next; //p结点的指针域指向要删除结点的指针域
- free(cur);
- return;
- }
- p = cur;
- cur = cur->next;
- }
- }
- }
- void RemoveAll(pLinkList list, DataType x)
- {
- pLinkNode cur = list->pHead;
- pLinkNode p = NULL;
- assert(list);
- if (NULL == list->pHead)
- {
- return;
- }
- while (NULL != cur)
- {
- if (NULL == list->pHead->next) //如果要只有一个结点
- {
- if (cur->data == x) //如果是则删除
- {
- list->pHead = cur->next;
- free(cur);
- return;
- }
- }
- else if (list->pHead->data == x) //判断是不是第一个结点,是则删除,继续判断
- {
- list->pHead = cur->next;
- free(cur);
- cur = list->pHead;
- }
- else
- {
- break;
- }
- }
- //要删除的结点在第一个结点之后
- cur = cur->next;
- p = list->pHead ;
- while (NULL != cur)
- {
- if (cur->data == x)
- {
- p->next = cur->next; //p结点的指针域指向要删除结点的指针域
- free(cur);
- cur = p;
- }
- p = cur;
- cur = cur->next;
- }
- }
- void Erase(pLinkList list, pLinkNode pos) //删除pos后面的结点
- {
- pLinkNode cur = list->pHead;
- pLinkNode p = list->pHead;
- assert(list);
- if (NULL == cur)
- {
- return;
- }
- if (NULL == cur->next) //如果只有一个结点
- {
- if (cur == pos)
- {
- free(cur);
- list->pHead = NULL;
- }
- }
- else
- {
- if (cur == pos) //如果是第一个结点
- {
- list->pHead = cur->next;
- free(cur);
- return;
- }
- cur = cur->next;
- while (NULL != cur)
- {
- if (cur == pos)
- {
- p->next = cur->next;
- free(cur);
- return;
- }
- p= cur;
- cur = cur->next;
- }
- }
- }
- void BubbleSort(pLinkList list)
- {
- pLinkNode cur = list->pHead;
- pLinkNode p1= list->pHead;
- int flag = 0;
- DataType tmp=0;
- pLinkNode p2 = list->pHead->next;
- assert(list);
- if (NULL == list->pHead)
- {
- return;
- }
- if (NULL == cur->next)
- {
- return;
- }
- cur = cur->next;
- while (NULL!=cur)
- {
- flag = 1;
- while (NULL != p2)
- {
- if (p1->data > p2->data)
- {
- tmp = p1->data;
- p1->data = p2->data;
- p2->data = tmp;
- flag = 0;
- }
- p2 = p2->next;
- p1 = p1->next;
- }
- if (flag)
- {
- break;
- }
- p1 = list->pHead;
- p2 = list->pHead->next;
- cur = cur->next;
- }
- }</span></strong>
test.c
[cpp] view plain copy
- <strong><span style="font-size:18px;">#include"linklist.h"
- void Menu()
- {
- printf("**********************************\n");
- printf("*0.Quit 1.InitLinkList *\n");
- printf("*2.PushBack 3.PopBack *\n");
- printf("*4.PushFront 5.PopFront *\n");
- printf("*6.PrintList 7.Find *\n");
- printf("*8.Insert 9.Remove *\n");
- printf("*10.RemoveAll 11.Erase *\n");
- printf("*12.BubbleSort \n");
- printf("**********************************\n\n");
- printf("请选择: ");
- }
- void test()
- {
- LinkList list;
- DataType x = 0;
- pLinkNode pos = NULL;
- int n = -1;
- while (1)
- {
- Menu();
- scanf("%d", &n);
- switch (n)
- {
- case 0:
- DestoryList(&list);
- exit(1);
- break;
- case 1:
- InitLinkList(&list);
- break;
- case 2:
- printf("请输入:");
- scanf("%d",&x);
- PushBack(&list,x);
- break;
- case 3:
- PopBack(&list);
- break;
- case 4:
- printf("请输入:");
- scanf("%d", &x);
- PushFront(&list,x);
- break;
- case 5:
- PopFront(&list);
- break;
- case 6:
- PrintList(&list);
- break;
- case 7:
- printf("请输入:");
- scanf("%d", &x);
- pos=Find(&list,x);
- printf("查找成功\n");
- break;
- case 8:
- printf("请输入元素:");
- scanf("%d", &x);
- Insert(&list,pos,x);
- break;
- case 9:
- printf("请输入:");
- scanf("%d", &x);
- Remove(&list,x);
- break;
- case 10:
- printf("请输入:");
- scanf("%d", &x);
- RemoveAll(&list,x);
- break;
- case 11:
- Erase(&list,pos);
- break;
- case 12:
- BubbleSort(&list);
- break;
- default:
- printf("选择无效,请重新选择\n");
- break;
- }
- }
- }
- int main()
- {
- test();
- system("pause");
- return 0;
- }</span></strong>
阅读全文
0 0
- C语言中链表的实现
- c语言中链表的实现
- c语言中链表的实现
- C语言数据结构中链表的具体实现
- C语言实现的井字棋
- C语言--容器的实现。
- C语言--迭代器的实现。
- 栈的C语言实现
- 汉诺塔的C语言实现
- C语言实现的俄罗斯方块
- ls的C语言实现
- 队列的C语言实现
- 单链表的C语言实现
- c语言readline的实现
- C语言状态机的实现
- 组合的C语言实现
- 栈的C语言实现
- CRC16的C语言实现
- Unity Shader 之Geometry Shader
- MFC-SpinControl(数值调节按钮)
- FastStone Cpture 8.5和Snagit 13.1.3使用比较
- 小红书学习——HTML简介:
- 不区分大小写的输入和ajax请求数据
- c语言中链表的实现
- JMeter处理返回结果unicode
- BLE中OSAL的运行机理怎么理解?
- bzoj水题
- 比特币脚本及其改进一例
- 二叉树最近公共父节点(记录根到某一节点路径)
- Elasticsearch 插件head 端增加一条记录和增加一个字段
- == 和equals 区别
- ubuntu 查看可用的shell和当前使用的shell