经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
C语言编写一个链表
来源:jb51  时间:2021/5/31 13:04:51  对本文有异议

本文实例为大家分享了C语言编写一个链表的具体代码,供大家参考,具体内容如下

链表

具备的基本功能:

1.创建头链表

  1. struct Node* Creatlist(){//创建链表头
  2. struct Node *headnode = (struct Node*)malloc(sizeof(struct Node));//创建动态内存链表,指针变量
  3. headnode->next = NULL;//链表初始化
  4. return headnode;
  5. }

2.创建节点

  1. struct Node* Creatnode(int num){//创建结点,链表,参数数字域
  2. struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));//创建动态内存链表,指针变量
  3. newnode->num = num;
  4. newnode->next = NULL;//链表初始化
  5. return newnode;
  6. }

3.插入节点

  1. void Insetlist(struct Node* list, int num){//头插法
  2. struct Node* insetnode = Creatnode(num);
  3. if (list != NULL){
  4. insetnode->next = list->next;
  5. list->next = insetnode;
  6. }
  7. else{
  8. printf("节点不存在\n");
  9. }
  10. }
  11. void Insetlists(struct Node* headnode, int n, int num){//在n节点处插入,参数头节点,在第n个节点处插,插入的数据num
  12. int i = 1;
  13. struct Node *t = headnode;
  14. while (i < n&& t!= NULL){
  15. t = t->next;
  16. i++;
  17. }
  18. if (t != NULL){
  19. struct Node* insetnode = Creatnode(num);
  20. insetnode->next = t->next;
  21. t->next = insetnode;
  22. }
  23. else{
  24. printf("节点不存在\n");
  25. }
  26. }

4.修改节点

  1. void Modifynode(struct Node* headnode, int n){//修改节点,参数链表,修改的第n个节点
  2. struct Node* list = headnode;
  3. int i = 0;
  4. while (i < n&&list != NULL){
  5. list = list->next;
  6. i++;
  7. }
  8. if (list != NULL){
  9. printf("请输入你要修改的值\n");
  10. int j = 0;
  11. scanf("%d", &j);
  12. list->num = j;
  13.  
  14. }
  15. else{
  16. printf("节点不存在\n");
  17. }
  18. }

5.删除节点

定义两个指针,一个指向删除节点的上一个节点,一个指向要删除的节点

  1. void Deletnode(struct Node* headnode, int n){//删除第n个节点,
  2. int i = 1;
  3. struct Node *strat = headnode;
  4. struct Node *end = headnode->next;
  5.  
  6. while (i < n&&end != NULL){
  7. strat = strat->next;
  8. end = end->next;
  9. i++;
  10. }
  11. if (end != NULL){
  12. strat->next = end->next;
  13. free(end);
  14.  
  15. }
  16. else{
  17. printf("节点不存在\n");
  18. }
  19. }

6.打印节点

  1. void Printnode(struct Node* headnode){//打印节点
  2. struct Node* list = headnode;
  3. while ((list->next) != NULL){
  4. list = list->next;
  5. printf("%d\t", list->num);
  6. }
  7. printf("\n");
  8.  
  9. }

7.主函数

  1. int main(){
  2. struct Node* list = Creatlist();
  3.  
  4. Insetlists(list, 1, 1);
  5. Printnode(list);
  6. int i = 0;
  7. printf("请输入修改哪个节点\n");
  8. scanf("%d", &i);
  9. Modifynode(list, i);
  10. Printnode(list);
  11. printf("请输入删除哪个节点\n");
  12. int n = 0;
  13. scanf("%d", &n);
  14. Deletnode(list, n);
  15. Printnode(list);
  16. system("pause");
  17. return 0;
  18. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号