数据结构之链表的增删改查逆置及传统链表与非传统链表
来源:互联网 发布:linux下zip 编辑:程序博客网 时间:2024/06/03 20:56
本质:结构体是一种构造数据类型;
用途:把不同类型的数据组合成一个整体-------自定义数据类型。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <windows.h>// 传统链表typedef struct Node{ int data; struct Node *next;}SLIST;// 不包含 datatypedef struct myNode{ struct Node *next;}myNode;// 非传统链表 业务节点大小可以不一样 缺点是计算节点其他数据的偏移量复杂typedef struct teacher{ int data; char name[64]; char name2[128]; struct Node node;}teacher;// 通用链表 企业链表typedef struct teacher{ struct Node node; int data; char name[64]; char name2[128];}teacher;void testAdv(){ teacher t1, t2, t3; t1.node.next = &(t2.node); t2.node.next = &(t3.node); t3.node.next = NULL;}SLIST *Create_SList();// 创建链表int Create_SList2(SLIST **mypHead);// 创建链表 优化int SList_Print(SLIST *pHead);// 遍历链表int SList_NodeInsert(SLIST *pHead, int x, int y);// 插入值 在x之前插入yint SList_NodeDel(SLIST *pHead, int y);int SList_Reverse(SLIST *pHead);int SList_Destory(SLIST *pHead);// 1、不停的malloc新节点 pM = malloc()// 2、新节点入链表 pCurrent->next = pM// 3、让新节点变成当前节点 pCurrent = pMSLIST *Create_SList(){ SLIST *pHead, *pM, *pCurrent; int data = 0; // 创建头节点并初始化 pHead = (SLIST *)malloc(sizeof(SLIST)); if(NULL == pHead) { return NULL; } pHead->data = 0; pHead->next = NULL; printf("\nPlease enter your data:"); scanf("%d",&data); pCurrent = pHead; while(-1 != data) { // 创建业务节点并初始化 pM = (SLIST *)malloc(sizeof(SLIST)); if(NULL == pM) { return NULL; } pM->data = data; pM->next = NULL; // 新节点入链表 pCurrent->next = pM; // 新节点变成当前节点 pCurrent = pM;// 链表节点的尾部追加 printf("\nPlease enter your data:"); scanf("%d",&data); } return pHead;}// 函数一个入口 多个出口(return)// 指针做函数参数int Create_SList2(SLIST **mypHead){ SLIST *pHead, *pM, *pCurrent; int data = 0; int ret = 0; // 创建头节点并初始化 pHead = (SLIST *)malloc(sizeof(SLIST)); if(NULL == pHead) { ret = -1; printf("func Create_SList2() error:%d \n",ret); goto END; } pHead->data = 0; pHead->next = NULL; printf("\nPlease enter your data:"); scanf("%d",&data); pCurrent = pHead; while(-1 != data) { // 创建业务节点并初始化 pM = (SLIST *)malloc(sizeof(SLIST)); if(NULL == pM) { ret = -2; goto END; } pM->data = data; pM->next = NULL; // 新节点入链表 pCurrent->next = pM; // 新节点变成当前节点 pCurrent = pM;// 链表节点的尾部追加 printf("\nPlease enter your data:"); scanf("%d",&data); }END: if(0 != ret) { SList_Destory(pHead); } else { *mypHead = pHead; } return ret;}int SList_Print(SLIST *pHead){ SLIST *ptmp = NULL; if(NULL == pHead) { return -1; } ptmp = pHead->next; printf("\nbegin\t"); while(ptmp) { printf("%d ",ptmp->data); ptmp = ptmp->next; } printf("\tend"); return 0;}// 链表是单向的,当前节点的位置保存在前驱节点的NEXT中int SList_NodeInsert(SLIST *pHead, int x, int y){ SLIST *pM, *pCurrent, *pPre; int data; // 创建新的节点pM pM = (SLIST *)malloc(sizeof(SLIST)); if(NULL == pM) { return -1; } pM->next = NULL; pM->data = y; //遍历链表 pPre = pHead; pCurrent = pHead->next; while(pCurrent) { if(pCurrent->data == x) { break; } pPre = pCurrent; pCurrent = pCurrent->next; } // 让新节点链接后续链表 pM->next = pPre->next; // 让前驱节点链接新节点 pPre->next = pM; return 0;}int SList_NodeDel(SLIST *pHead, int y){ SLIST *pCurrent, *pPre; // 初始化状态 pPre = pHead; pCurrent = pHead->next; // 遍历链表 while(NULL != pCurrent) { if(pCurrent->data == y) { break; } pPre = pCurrent; pCurrent = pCurrent->next; } // 删除操作 if(NULL == pCurrent) { printf("Not found %d",y); return -1; } pPre->next = pCurrent->next; if(NULL != pCurrent) { free(pCurrent); } return 0;}int SList_Destory(SLIST *pHead){ SLIST *tmp = NULL; if(NULL == pHead) { return -1; } while(pHead != NULL) { tmp = pHead->next;// 缓存头节点 free(pHead); pHead = tmp; } return 0;}int SList_Reverse(SLIST *pHead){ SLIST *p = NULL;// 前驱指针 SLIST *q = NULL;// 当前指针 SLIST *t = NULL;// 缓存的一个节点 if(NULL == pHead || NULL == pHead->next || NULL == pHead->next->next) { return 0; } // 前驱节点 // p = pHead; // q = pHead->next; p = pHead->next; q = pHead->next->next; // 一个一个节点的逆置 while(q) { t = q->next; // 缓冲节点 q->next = p;// 逆置 p = q;// 让p下移一个节点 q = t; } // 头节点变成尾部节点后置为NULL pHead->next->next = NULL; pHead->next = p; return 0;}int main(){ SLIST *pHead = NULL; int ret = 0; // 创建链表并打印新链表 pHead = Create_SList(); ret = SList_Print(pHead); // 插入节点并打印新链表 ret = SList_NodeInsert(pHead, 20, 19); ret = SList_Print(pHead); // 删除节点并打印新链表 ret = SList_NodeDel(pHead, 19); ret = SList_Print(pHead); // 逆置链表节点并打印 ret = SList_Reverse(pHead); ret = SList_Print(pHead); // 销毁链表 ret = SList_Destory(pHead); system("pause"); return 0;}
阅读全文
0 0
- 数据结构之链表的增删改查逆置及传统链表与非传统链表
- 数据结构——单向传统链表(java实现)
- 数组的输出(传统方式与非传统方式)
- Linux内核链表Vs传统链表
- Linux内核链表VS传统链表
- 数据结构之链表的增删操作的Java实现
- 区块链与传统分布式数据库的区别
- SpringMVC 配置之浅谈传统的非注解控制器
- 数据结构之链表实现增删改查
- 重学数据结构 链表增删
- AI 与区块链会碰撞出什么样的火花?要取代传统银行系统!?
- 传统商业智能的局限性及未来发展之路!
- Hive与传统的数据库
- fetch与传统的ajax
- 未知的世界(非传统题)
- 关键链方法和传统关键路径方法的比较
- 使用传统hbase的api创建hbase表(scala)
- 云输入法与传统输入法的整合之道
- gradle buildTypes
- REST和RESTFUL的相关概念理解
- datagrid怎么使用
- MyBatis 配置2
- ReentrantLock的公平锁和非公平锁
- 数据结构之链表的增删改查逆置及传统链表与非传统链表
- Third week of machine learning on Coursera
- re+lxml+requests基础知识
- 设计模式
- spring装配Bean
- Elasticsearch(一)安装
- NYOJ 94 cigarettes
- 数组内置方法
- 吴恩达深度学习课后编程题讲解(python)