鱼C论坛

 找回密码
 立即注册
查看: 2608|回复: 1

[学习笔记] 循环链表初始化、随机20个数值、打印一圈、10圈、指定插入、指定删除、结束

[复制链接]
发表于 2021-9-15 00:39:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
源程序:


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. #define MAXSIZE 20

  5. typedef struct like
  6. {
  7.         int T;
  8.         struct like *next;
  9. }like, *likes;

  10. void explain();
  11. void InitList(likes *G);
  12. void GetList(likes *G);
  13. void PutList(like *G);
  14. void PutTwoList(like *G);
  15. void AppointList(likes *G);
  16. void DeleList(likes *G);
  17. void DeleList(likes *G)
  18. {
  19.         like *temp;
  20.         like *temps = (*G)->next;
  21.         int A;
  22.        
  23.         scanf("%d", &A);
  24.         getchar();
  25.         printf("删除数值%d", A);
  26.        
  27.         while (1)
  28.         {
  29.                 if (temps->T == A)
  30.                 {
  31.                         if ((*G)->next == temps)
  32.                         {
  33.                                 temp = temps;
  34.                                 do
  35.                                 {
  36.                                         temp = temp->next;
  37.                                 } while (temp->next !=(*G)->next);

  38.                                 (*G)->next = temps->next;        //头部数据调整
  39.                                 temp->next = temps->next;        //尾部数据调整
  40.                         }
  41.                         else
  42.                         {
  43.                                 temp->next = temps->next;
  44.                         }
  45.                         free(temps);
  46.                         break;
  47.                 }
  48.                 temp = temps;        //保留上一个链表的地址
  49.                 temps = temps->next;
  50.         }

  51. }

  52. void AppointList(likes *G)
  53. {
  54.         like *temp;
  55.         like *temps = (*G)->next;
  56.         int A;

  57.         scanf("%d", &A);
  58.         getchar();
  59.         printf("在%d插入数值:", A);

  60.         while (1)
  61.         {
  62.                 if (temps->T == A)
  63.                 {
  64.                         temp = (likes )malloc(sizeof(like ));
  65.                         scanf("%d", &temp->T);
  66.                         getchar();
  67.                         temp->next = temps->next;
  68.                         temps->next = temp;
  69.                         break;
  70.                 }
  71.                 temps = temps->next;
  72.         }

  73. }

  74. void PutTwoList(like *G)
  75. {
  76.         int i;
  77.         like *temp = G->next;
  78.        
  79.         for (i = 0; i < 10; i++ )
  80.         {
  81.                 printf("%d、", i + 1);
  82.                 do
  83.                 {
  84.                         printf("<%d>", temp->T);
  85.                         temp = temp->next;
  86.                 } while (G->next != temp );
  87.                 putchar('\n');
  88.         }
  89. }

  90. void PutList(like *G)
  91. {
  92.         like *temp = G->next;

  93.         do
  94.         {
  95.                 printf("<%d>", temp->T);
  96.                 temp = temp->next;
  97.         } while (G->next != temp);
  98. }

  99. void GetList(likes *G)
  100. {
  101.         like *temp, *tail;
  102.         int i;

  103.         //随机创建20个数值
  104.         srand((unsigned)time(NULL));
  105.         for (i = 0; i < MAXSIZE; i++)
  106.         {
  107.                 temp = (likes )malloc(sizeof(like ));
  108.                
  109.                 temp->T = (rand() % 100);
  110.                 if (*G == (*G)->next)
  111.                 {
  112.                         temp->next = temp;
  113.                         (*G)->next = temp;
  114.                 }
  115.                 else
  116.                 {
  117.                         tail->next = temp;
  118.                         temp->next = (*G)->next;
  119.                 }
  120.                 tail = temp;
  121.         }
  122.        
  123. }       

  124. void InitList(likes *G)                //初始化链表
  125. {
  126.         (*G)->next = *G;
  127. }

  128. void explain()
  129. {
  130.         printf("1、初始化循环链表\n");
  131.         printf("2、输入数据\n");
  132.         printf("3、打印一圈循环链表\n");
  133.         printf("4、无限制打印循环链表\n");
  134.         printf("5、指定位置插入数据\n");
  135.         printf("6、指定位置删除数据\n");
  136.         printf("0、结束本程序\n");
  137. }
  138. int main()
  139. {
  140.        
  141.         int L;
  142.         like *G = (likes )malloc(sizeof(like ));

  143.         explain();

  144.         while (1)
  145.         {
  146.                 printf("输入选项:");
  147.                 scanf("%d", &L);
  148.                 getchar();

  149.                 switch (L)
  150.                 {
  151.                 case 1:{
  152.                         InitList(&G);
  153.                            }
  154.                         break;
  155.                 case 2:{
  156.                         GetList(&G);
  157.                            }
  158.                         break;
  159.                 case 3:{
  160.                         PutList(G);
  161.                         putchar('\n');
  162.                            }
  163.                         break;
  164.                 case 4:{
  165.                         PutTwoList(G);
  166.                            }
  167.                         break;
  168.                 case 5:{
  169.                         AppointList(&G);
  170.                            }
  171.                         break;
  172.                 case 6:{
  173.                         DeleList(&G);
  174.                         putchar('\n');
  175.                            }
  176.                         break;
  177.                 case 0:{
  178.                         return 0;
  179.                            }
  180.                         break;
  181.                 }
  182.         }
  183.        
  184.         return 0;
  185. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-3 20:13:54 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 06:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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