鱼C论坛

 找回密码
 立即注册
查看: 3616|回复: 0

[学习笔记] 初始化链表、头插链表、尾插链表、两个链表相连

[复制链接]
发表于 2021-9-28 18:23:37 | 显示全部楼层 |阅读模式

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

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

x
相连后我就不释放N链表的头节点了我这从新建立了一个K链表返回之后付给新新链表

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

  4. #define MAXSIZE 10

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


  10. void Explain();     //程序说明
  11. void InitList(Likes *N);    //初始化及清除数据
  12. void TailList(Likes *M);     //创建尾插法链表
  13. void HeadList(Likes *N);     //创建尾插法链表
  14. void AlPuList(Like *N);     //打印链表
  15. struct Like *LinkList(Like *N, Like *M); //两个链表合并

  16. struct Like *LinkList(Like *N, Like *M)
  17. {
  18.     Like *temp = N->next->next;     //N的一号节点传给临时变量
  19.     N->next->next = M->next->next;  //M的1号节点传给N尾节点之后
  20.     M->next->next = temp;           //N1号节点传给M节点之后也就是当M的一号数据

  21.     return M->next;
  22. }

  23. void AlPuList(Like *N)
  24. {
  25.     Like *temp = N->next;

  26.     do
  27.     {
  28.         printf("<%d>", temp->T);
  29.         temp = temp->next;

  30.     }while(temp != N->next);
  31.     putchar('\n');

  32. }

  33. void TailList(Likes *M)
  34. {
  35.     Like *temp, *tail;
  36.     srand((unsigned)time(NULL));        //随意创建N个链表

  37.     for(int i = 0; i < MAXSIZE; i++)
  38.     {
  39.         temp = (Likes )malloc(sizeof(Like ));
  40.         temp->T = (rand() % 100);
  41.         temp->next = (*M)->next;

  42.         if((*M)->next == *M)
  43.         {
  44.             (*M)->next = temp;
  45.         }
  46.         else
  47.         {
  48.             tail->next = temp;
  49.         }
  50.         tail = temp;
  51.     }
  52.     (*M)->next = tail;

  53. }
  54. void HeadList(Likes *N)
  55. {
  56.     Like *temp, *tail;
  57.     srand((unsigned)time(NULL));

  58.     for(int i = 0; i < MAXSIZE; i++)
  59.     {
  60.         temp = (Likes )malloc(sizeof(Like ));
  61.         temp->T = (rand() % 100);

  62.         if((*N)->next == *N )
  63.         {
  64.             (*N)->next = temp;
  65.             temp->next = temp;  //自己指向自己
  66.             tail = temp;        //把尾链表传给tail,(tail不做更改)
  67.         }
  68.         else
  69.         {
  70.             temp->next = (*N)->next;    //把链表1号节点传给临时链表temp->next
  71.             (*N)->next = temp;          //把temp替换1号节点的数据因为前面已经做了向下一个的next所以链表是相连的
  72.             tail->next = temp;          //把尾节点的指向next替换成新的1号节点点形成循环
  73.         }
  74.     }
  75.     (*N)->next = tail;                  //把链表的头节点指向链表的尾节点

  76. }

  77. void InitList(Likes *N)
  78. {
  79.     Like *temp,*temps;

  80.   if(*N != NULL)
  81.     {
  82.         temp = (*N)->next;
  83.         do
  84.         {
  85.             temps = temp;
  86.             temp = temp->next;
  87.             free(temps);
  88.         }while((*N)->next != temp);
  89.     }
  90.     else
  91.     {
  92.         *N = (Likes )malloc(sizeof(Like ));
  93.     }
  94.     (*N)->next = *N;




  95. }
  96. void Explain()
  97. {
  98.     printf("1、初始化程序及清除数据\n");
  99.     printf("2、创建头插法链表\n");
  100.     printf("3、创建尾插法链表\n");
  101.     printf("4、两个链表合并\n");
  102.     printf("9、打印全部链表\n");
  103.     printf("0、结束本程序\n");
  104. }

  105. int main()
  106. {
  107.     int i;
  108.     Like *N = NULL, *M = NULL, *K;

  109.     Explain();

  110.     while(1)
  111.     {
  112.         printf("输入选项:");
  113.         scanf("%d",&i);

  114.         switch(i)
  115.         {
  116.             case 1:{
  117.                 InitList(&N);
  118.                 InitList(&M);
  119.             }
  120.             break;
  121.             case 2:{
  122.                 HeadList(&N);
  123.             }
  124.             break;
  125.             case 3:{
  126.                 TailList(&M);
  127.             }
  128.             break;
  129.             case 4:{
  130.                 K = LinkList(N, M);
  131.                 AlPuList(K);
  132.             }
  133.             break;
  134.              case 9:{
  135.                AlPuList(N);
  136.                AlPuList(M);
  137.             }
  138.             break;
  139.             case 0:{
  140.                 return 0;
  141.             }
  142.             break;
  143.         }

  144.     }

  145.     return 0;
  146. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-12 17:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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