欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

C语言 数据结构 链表的增删查改

发布时间:2024/2/28 编程问答 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C语言 数据结构 链表的增删查改 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

分别用函数实现了链表的:
1、增加(头插法,尾插法,有序插入)
2、删除
3、修改
4、查找

C代码

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> typedef struct student {int num;float score;struct student *pnext; }stu, *pstu;//打印 void list_print(pstu phead, pstu ptail) {pstu pcur = phead;while (pcur != NULL){printf("%d %2.2f\n", pcur->num, pcur->score);pcur = pcur->pnext;}printf("\n"); }//增:头插法 void list_insert_head(pstu *pphead, pstu *pptail, int i) {pstu pnew;pnew = (pstu)calloc(1, sizeof(stu));pnew->num = i;if (NULL == *pptail)//如果链表为空{*pphead = pnew;*pptail = pnew;//不用担心尾节点不是NULL,因为calloc的pnew中的pnext成员本身就是null}else{pnew->pnext = *pphead;//新节点的pnext成员指向原有链表头*pphead = pnew;//新节点成为新的链表头} }//增:尾插法 void list_insert_tail(pstu *pphead, pstu *pptail, int i) {pstu pnew;pnew = (pstu)calloc(1, sizeof(stu));pnew->num = i;if (NULL == *pptail)//如果链表为空{*pphead = pnew;*pptail = pnew;//不用担心尾节点不是NULL,因为calloc的pnew中的pnext成员本身就是null}else{(*pptail)->pnext = pnew;//原有链表尾的pnext成员指向新节点*pptail = pnew;//新节点成为新的链表尾} }//增:有序插入 void list_insert_sort(pstu *pphead, pstu *pptail, int i) {pstu pnew;pnew = (pstu)calloc(1, sizeof(stu));pnew->num = i;pstu pcur = *pphead;pstu pbefore = *pphead;if (NULL == *pptail)//如果链表为空{*pphead = pnew;*pptail = pnew;//不用担心尾节点不是NULL,因为calloc的pnew中的pnext成员本身就是null}else{//从小到大if (i <= pcur->num)//如果小于第一个节点,头插{pnew->pnext = *pphead;//新节点的pnext成员指向原有链表头*pphead = pnew;//新节点成为新的链表头return;}else//向后遍历{while (NULL != pcur)//只要不是最后{if (i <= pcur->num){pbefore->pnext = pnew;//小弟的pnext成员指向新节点pnew->pnext = pcur;//新节点的pnext成员指向大哥break;}pbefore = pcur;//小弟记住大哥的脚印pcur = pcur->pnext;//大哥先走一步 }if (NULL == pcur)//如果到最后,尾插{(*pptail)->pnext = pnew;//原有链表尾的pnext成员指向新节点*pptail = pnew;//新节点成为新的链表尾}}} }//删除 void list_delete(pstu *pphead, pstu *pptail, int i) {pstu pcur = *pphead;pstu pbefore = *pphead;if (NULL == *pptail)//如果链表为空{printf("链表为空\n");}else if (i == (*pphead)->num)//如果等于第一个节点(此时可能仅剩一个节点){*pphead = (*pphead)->pnext;//头指针指向下一个节点if (NULL == *pphead){*pptail = NULL;free(pcur);}}else//如果不等于第一个节点{while (NULL != pcur)//只要不是最后{if (i == pcur->num)//如果等于当前节点{pcur = pcur->pnext;//大哥前进一步pbefore->pnext = pcur;//小弟的pnext成员指向大哥return;}else{pbefore = pcur;//小弟记住大哥的脚印pcur = pcur->pnext;//大哥先走一步 }}if (NULL == pcur)//如果到最后{printf("不存在此数字\n");}} }//修改 void list_modify(pstu *pphead, pstu *pptail, int i, float score) {pstu pcur = *pphead;pstu pbefore = *pphead;if (NULL == *pptail)//如果链表为空{printf("链表为空\n");}else{while (NULL != pcur)//只要不是最后{if (i == pcur->num)//如果等于当前节点,修改{pcur->score = score;return;}else{pbefore = pcur;//小弟记住大哥的脚印pcur = pcur->pnext;//大哥先走一步 }}if (NULL == pcur)//如果到最后{printf("不存在此数字\n");}} } //查找 void list_find(pstu *pphead, pstu *pptail, int i) {pstu pcur = *pphead;pstu pbefore = *pphead;if (NULL == *pptail)//如果链表为空{printf("链表为空\n");}else{while (NULL != pcur)//只要不是最后{if (i == pcur->num)//如果等于当前节点,输出{printf("查询成功:\n%d %2.2f\n", i, pcur->score);return;}else{pbefore = pcur;//小弟记住大哥的脚印pcur = pcur->pnext;//大哥先走一步 }}if (NULL == pcur)//如果到最后{printf("不存在此数字\n");}} }int main() {int i;float score;pstu phead = NULL;pstu ptail = NULL;//增printf("请输入你要插入的数字:");while (scanf("%d", &i) != EOF){// list_insert_head(&phead, &ptail, i);//头插法// list_insert_tail(&phead, &ptail, i);//尾插法list_insert_sort(&phead, &ptail, i);//有序插入}list_print(phead, ptail);//删printf("请输入你要删除的数字:");while (scanf("%d", &i) != EOF){list_delete(&phead, &ptail, i);printf("输出:\n");list_print(phead, ptail);}//改 printf("请输入你要修改的序号和数字:");while (scanf("%d %f", &i, &score) != EOF){list_modify(&phead, &ptail, i, score);printf("输出:\n");list_print(phead, ptail);}//查printf("请输入你要查找的序号:");while (scanf("%d", &i) != EOF){list_find(&phead, &ptail, i);}system("pause"); } 超强干货来袭 云风专访:近40年码龄,通宵达旦的技术人生

总结

以上是生活随笔为你收集整理的C语言 数据结构 链表的增删查改的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。