鱼C论坛

 找回密码
 立即注册
查看: 3840|回复: 10

C 语言结构指针,请大牛帮忙看看链表的创建和增删功能的代码。

[复制链接]
发表于 2013-1-30 17:38:35 | 显示全部楼层 |阅读模式
5鱼币
C 语言结构指针,请大牛帮忙看看链表的创建和增删功能的代码。
帮忙看看问题出在什么问题,老卡死。
另外,帮忙看看,是不是还可以再优化?
在此深表感激!
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>#define LEN sizeof(struct stu)int n=0;//*****************************************************************
  4. //定义一个学生结构stu,参数只包含了学号和成绩,num,score
  5. //还有一个指向下一个结点的next指针
  6. //*****************************************************************struct stu
  7. {
  8. int num;
  9. int score;
  10. struct stu *next;
  11. };//*****************************************************************
  12. //声明主要用到的函数,创建链表,及对链表的增删改操作的函数
  13. //*****************************************************************
  14. struct stu *create(); //创建
  15. void print(struct stu *head);//输出
  16. struct stu *del(struct stu *head,int num); //删除节点
  17. struct stu *inser(struct stu *head,struct stu *stu_2);//插入节点
  18. //*****************************************************************
  19. //主函数MAIN()
  20. //*****************************************************************
  21. void main()
  22. {
  23. //定义一个结构指针,用来接收创建的链表
  24. struct stu *T;
  25. int num;
  26. T = create(); //打印输出链表
  27. print(T); //删除节点
  28. printf("Please input the num that You want to deleted:");
  29. scanf("%d",&num);
  30. T = del(T,num);
  31. print(T); //插入节点

  32. printf("Please input the data of insert!\n"); struct stu *stu_2; stu_2 = (struct stu *)malloc(LEN); printf("Please input the data of num:");
  33. scanf("%d",&stu_2->num);
  34. printf("Please input the data of score:");
  35. scanf("%d",&stu_2->score);
  36. T = inser(T,stu_2);
  37. print(T);


  38. system("pause");
  39. }
  40. //*****************************************************************
  41. //创建表
  42. //*****************************************************************
  43. struct stu *create()
  44. {
  45. struct stu *head;//头指针
  46. struct stu *p1,*p2;//动态指针,p2跟随p1,保存p1的足迹,以备所需 //用户输入数据前,要手动为结构开辟一个结构空间,并将p1,p2指向此结构地址
  47. p1 = p2 = (struct stu *)malloc(LEN);
  48. //提示用户输入数据
  49. printf("Please input the data of num:");
  50. scanf("%d",&p1->num);
  51. printf("Please input the data of score:");
  52. scanf("%d",&p1->score); head = NULL;
  53. while(0 != p1->num)
  54. {
  55.   n++;
  56.   if(1 == n)
  57.   {
  58.    head = p1;
  59.   }
  60.   else
  61.   {
  62.    p2->next = p1;
  63.    
  64.   }
  65.   p2 = p1;//此处注意:必须先将p1指向下一个结点,再把p2跟过来,最后开辟新的内存空间
  66.    
  67.   //开避下一个节点内存空间
  68.   p1 = (struct stu *)malloc(LEN);  printf("Please input the data of num:");
  69.   scanf("%d",&p1->num);
  70.   printf("Please input the data of score:");
  71.   scanf("%d",&p1->score);
  72. }
  73. p2->next = NULL;
  74. return head;
  75. }//*****************************************************************
  76. //打印表
  77. //*****************************************************************
  78. void print(struct stu *head)
  79. {
  80. //定义指针结构变量来接收链表
  81. struct stu *p;
  82. p = head; printf("共有%d条记录被统计\n",n); if(NULL != head)
  83. {
  84.   while (NULL != p)
  85.   {
  86.    printf("\n该学生学号是:%d,成绩是:%d \n",p->num,p->score);
  87.    p = p->next;
  88.   };
  89. }
  90. }//*****************************************************************
  91. //删除节点
  92. //*****************************************************************
  93. struct stu *del(struct stu *head,int num)
  94. {
  95. struct stu *p1,*p2; if(NULL != head)
  96. {
  97.   p1 = head;  while(num != p1->num && NULL != p1->next)
  98.   {
  99.    
  100.    p2 = p1;
  101.    p2->next = p1;
  102.   }  if(num == p1->num)
  103.   {
  104.    if(head = p1)
  105.    {
  106.     head = p1->next;
  107.    }
  108.    else
  109.    {
  110.     p2->next = p1->next;
  111.    }
  112.    printf("Delete No.%d SUCCEDD!\n",num);
  113.   }
  114.   else
  115.   {
  116.    printf("No.%d is not found!",num);
  117.   }
  118.   n = n - 1;
  119. }
  120. else
  121. {
  122.   printf("This is a Null table!");
  123.   head = NULL;
  124. }

  125. return head;
  126. }//*****************************************************************
  127. //插入节点
  128. //*****************************************************************
  129. struct stu *inser(struct stu *head,struct stu *stu_2)
  130. {
  131. struct stu *p0;
  132. struct stu *p1,*p2; p0 = stu_2;
  133. p1 = head; if(NULL == head)
  134. {
  135.   head = p0;//如果是空的链表,就将头指针指向此将要插入的节点
  136.   p0->next = NULL;
  137. }
  138. else
  139. {
  140.   while((p0->num > p1->num) && (NULL != p1->next)) //如果学号小于或等于下一个节点的学号且,下一个节点非空,就继续遍历此表
  141.   {
  142.    
  143.    p2 = p1;
  144.    p2->next = p1;
  145.   }  if(p0->num <= p1->num) //如果将要插入的节点的学号等于下个节点的学号,且此节点为头节点,则将head指向p0。即表头插入。
  146.   {
  147.    if(head == p1)
  148.    {
  149.     head = p0;
  150.    }
  151.    else //中间插入
  152.    {
  153.    
  154.     p2->next = p0;
  155.    
  156.    }
  157.    p0->next = p1;
  158.   }
  159.   else //表尾插入
  160.   {
  161.    p1->next = p0;
  162.    p0->next = NULL;
  163.   }
  164. }
  165. n = n + 1;
  166. return head;
  167. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-30 17:38:36 | 显示全部楼层
本帖最后由 消失在黑暗中 于 2013-1-31 20:26 编辑



  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #define LEN sizeof(struct stu)
  5. int n=0;
  6. //*****************************************************************
  7. //定义一个学生结构stu,参数只包含了学号和成绩,num,score
  8. //还有一个指向下一个结点的next指针
  9. //*****************************************************************
  10. struct stu
  11. {
  12.         int num;
  13.         int score;
  14.         struct stu *next;
  15. };//*****************************************************************
  16. //声明主要用到的函数,创建链表,及对链表的增删改操作的函数
  17. //*****************************************************************
  18. struct stu *create(); //创建
  19. void print(struct stu *head);//输出
  20. struct stu *del(struct stu *head,int num); //删除节点
  21. struct stu *inser(struct stu *head,struct stu *stu_2);//插入节点
  22. //*****************************************************************
  23. //主函数MAIN()
  24. //*****************************************************************
  25. void main()
  26. {
  27.         //定义一个结构指针,用来接收创建的链表
  28.         struct stu *T;
  29.         int num;
  30.         T = create(); //创建链表
  31.         print(T); //打印链表
  32.         printf("Please input the num that You want to deleted:");
  33.         scanf("%d",&num);
  34.         T = del(T,num);//删除节点
  35.         print(T); //打印链表
  36.         printf("Please input the data of insert!\n");
  37.         struct stu *stu_2;
  38.         stu_2 = (struct stu *)malloc(LEN);
  39.         printf("Please input the data of num:");
  40.         scanf("%d",&stu_2->num);
  41.         printf("Please input the data of score:");
  42.         scanf("%d",&stu_2->score);
  43.         T = inser(T,stu_2);
  44.         print(T);
  45.         system("pause");
  46. }
  47. //*****************************************************************
  48. //创建表
  49. //*****************************************************************
  50. struct stu *create()//修改了这个函数,按照楼主的意思,如果输入的num等于0,结束链表的添加,此函数至少返回
  51. //一个头结点
  52. {
  53.         struct stu *head = NULL;//头指针
  54.         struct stu *p1,*p2;//动态指针,p2跟随p1,保存p1的足迹,以备所需 //用户输入数据前,要手动为结构开辟一个结构空间,并将p1,p2指向此结构地址
  55.         while(1)
  56.         {
  57.                 p1 = (struct stu *)malloc(LEN);
  58.                 printf("Please input the data of num:");
  59.                         scanf("%d",&p1->num);
  60.                 if(head == NULL)
  61.                 {
  62.                      p2  =   head = p1;
  63.                 }
  64.                 else
  65.                 {
  66.                         p2->next = p1;
  67.                 }
  68.                 p1->next = NULL;
  69.                 if (p1->num == 0)//假如输入的学号为0,结束链表的添加
  70.                 {
  71.                                  p2->next = NULL;
  72.                                 break;
  73.               }
  74.               n++;
  75.                 printf("Please input the data of score:");
  76.                 scanf("%d",&p1->score);
  77.                 p2 = p1;//此处注意:必须先将p1指向下一个结点,再把p2跟过来,最后开辟新的内存空间
  78.         }
  79.         return head;
  80. }//*****************************************************************
  81. //打印表
  82. //*****************************************************************
  83. void print(struct stu *head)
  84. {
  85.         //定义指针结构变量来接收链表
  86.         struct stu *p;
  87.         p = head;
  88.         printf("共有%d条记录被统计\n",n);
  89.         if(NULL != head)
  90.         {
  91.                 while (NULL != p)
  92.                 {
  93.                         printf("\n该学生学号是:%d,成绩是:%d \n",p->num,p->score);
  94.                         p = p->next;
  95.                 };
  96.         }
  97. }//*****************************************************************
  98. //删除节点
  99. //*****************************************************************
  100. struct stu *del(struct stu *head,int m_num)//修改了删除函数
  101. {
  102.         struct stu *p1,*p2;
  103.         p1 = head;
  104.         if (head != NULL)
  105.         {
  106.                 while (m_num != p1->num)
  107.                 {
  108.                         p2 =p1;//保存节点指针
  109.                         p1 = p1->next;//获取下一个节点地址
  110.                         if (p1 == NULL)
  111.                         {
  112.                                 printf("No.%d is not found!",m_num);
  113.                                 return head;
  114.                         }//找不到节点,返回head
  115.                 }
  116.                 if (p1 == head)
  117.                         head = p1->next;
  118.                 else
  119.                         p2->next = p1->next;
  120.                 free(p1);//释放删除的节点
  121.                 n--;
  122.         }
  123.         return head;
  124. }//*****************************************************************
  125. //插入节点
  126. //*****************************************************************
  127. struct stu *inser(struct stu *head,struct stu *stu_2)//我试了下插入的功能,能正常插入,所以没改
  128. {
  129.         struct stu *p0;
  130.         struct stu *p1,*p2; p0 = stu_2;
  131.         p1 = head;
  132.         if(NULL == head)
  133.         {
  134.                 head = p0;//如果是空的链表,就将头指针指向此将要插入的节点
  135.                 p0->next = NULL;
  136.         }
  137.         else
  138.         {
  139.                 while((p0->num > p1->num) && (NULL != p1->next)) //如果学号小于或等于下一个节点的学号且,下一个节点非空,就继续遍历此表
  140.                 {
  141.                         
  142.                         p2 = p1;
  143.                         p2->next = p1;
  144.                 }  if(p0->num <= p1->num) //如果将要插入的节点的学号等于下个节点的学号,且此节点为头节点,则将head指向p0。即表头插入。
  145.                 {
  146.                         if(head == p1)
  147.                         {
  148.                                 head = p0;
  149.                         }
  150.                         else //中间插入
  151.                         {
  152.                                 
  153.                                 p2->next = p0;
  154.                                 
  155.                         }
  156.                         p0->next = p1;
  157.                 }
  158.                 else //表尾插入
  159.                 {
  160.                         p1->next = p0;
  161.                         p0->next = NULL;
  162.                 }
  163.         }
  164.         n = n + 1;
  165.         return head;
  166. }
复制代码

评分

参与人数 1鱼币 +2 贡献 +1 收起 理由
lyoal + 2 + 1

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-1-30 17:46:17 | 显示全部楼层
ee。。。看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-1-30 18:32:42 | 显示全部楼层
有够乱的,卡住是程序有问题哈。
del的时候,修改代码:
  1. //*****************************************************************
  2. //删除节点
  3. //*****************************************************************
  4. struct stu *del(struct stu *head,int num)
  5. {       
  6.         struct stu *p1,*p2;
  7.         if(NULL != head)
  8.         {
  9.                 p1 = head;  
  10.                 while(num != p1->num && NULL != p1->next)
  11.                 {   
  12.                         p2 = p1;
  13.                         p1 = p1->next;                       
  14.                         //p2->next = p1;
  15.                 }  
  16.                 if(num == p1->num)
  17.                 {
  18.                         p2->next = p1->next;
  19.                         /*if(head = p1)
  20.                         {
  21.                                 head = p1->next;
  22.                         }else
  23.                         {
  24.                                 p2->next = p1->next;
  25.                         }*/                       
  26.                         printf("Delete No.%d SUCCEDD!\n",num);
  27.                         n = n - 1;
  28.                 }else
  29.                 {
  30.                         printf("No.%d is not found!",num);
  31.                 }
  32.                
  33.         }else
  34.         {
  35.                 printf("This is a Null table!");
  36.                 head = NULL;
  37.         }
  38.         return head;
  39. }
复制代码
增加元数的时候,修改代码:

  1. //*****************************************************************
  2. //插入节点
  3. //*****************************************************************
  4. struct stu *inser(struct stu *head,struct stu *stu_2)
  5. {
  6.         struct stu *p0;
  7.         struct stu *p1,*p2;
  8.         p0 = stu_2;
  9.         p1 = head;
  10.         if(NULL == head)
  11.         {
  12.                 head = p0;//如果是空的链表,就将头指针指向此将要插入的节点
  13.                 p0->next = NULL;
  14.         }else
  15.         {
  16.                 //寻找最末端
  17.                 while(NULL!=p1->next)
  18.                 {
  19.                         p1 = p1->next;
  20.                 }
  21.                 //添加元数
  22.                 p1->next = p0;
  23.                 p0->next = NULL;

  24.                 /*
  25.                 while((p0->num > p1->num) && (NULL != p1->next)) //如果学号小于或等于下一个节点的学号且,下一个节点非空,就继续遍历此表
  26.                 {   
  27.                         p2 = p1;
  28.                         p2->next = p1;                       
  29.                 }  
  30.                 if(p0->num <= p1->num) //如果将要插入的节点的学号等于下个节点的学号,且此节点为头节点,则将head指向p0。即表头插入。
  31.                 {
  32.                         if(head == p1)
  33.                         {
  34.                                 head = p0;
  35.                         }else //中间插入
  36.                         {   
  37.                                 p2->next = p0;   
  38.                         }
  39.                         p0->next = p1;
  40.                 }else //表尾插入
  41.                 {
  42.                         p1->next = p0;
  43.                         p0->next = NULL;
  44.                 }
  45.                 */
  46.         }
  47.         n = n + 1;
  48.         return head;
  49. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-1-30 22:47:00 | 显示全部楼层

非常感谢,可能我说的不太清楚,这个对链表的创建后,对链表的节点的删除,插入操作中,插入分头部,中间,尾部三种插入情况,但经这样一改只剩追加节点的操作了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-1-31 16:58:29 | 显示全部楼层
我是来刷墙的

                               
登录/注册后可看大图





















                               
登录/注册后可看大图

防辐射服鉴别
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-2-2 14:56:26 | 显示全部楼层
牛人啊,太牛逼了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-2-2 19:24:53 | 显示全部楼层
赚取鱼币了喽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-2-2 20:49:05 | 显示全部楼层
赚取鱼币了喽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-2-3 12:40:34 | 显示全部楼层
没有  钱了  找钱
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-6-4 23:40:39 | 显示全部楼层
没有鱼比了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 09:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表