• 链表的创建、测长、排序、插入、逆序的实现(C语言)


    1. #include <stdio.h>
    2. #define END_elem 0
    3. struct node
    4. {
    5. int date;
    6. struct node * next;
    7. };
    8. //链表创建
    9. node * creat()
    10. {
    11. int x;
    12.     node *head,*last,*p;
    13.     head=new node();
    14.     last=head;
    15. scanf("%d",&x);
    16. while(x!=END_elem)
    17. {
    18.         p=new node();
    19.         p->date=x;
    20.         last->next=p;
    21.         last=p;
    22. scanf("%d",&x);
    23. }
    24.     head=head->next;
    25.     last->next=NULL;
    26. return head;
    27. }
    28. //单链表测长
    29. int lenght(node *head)
    30. {
    31. int n=0;
    32.     node *p=head;
    33. while(p!=NULL)
    34. {
    35.         p=p->next;
    36.         n++;
    37. }
    38. return n;
    39. }
    40. //链表排序
    41. node *sort(node *head,int n)
    42. {
    43. int temp;
    44.     node *p;
    45. if(head==NULL||head->next==NULL)
    46. return head;
    47. for(int i=0;i<n-1;i++)
    48. {
    49.         p=head;
    50. for(int j=0;j<n-1-i;j++)
    51. {
    52. if(p->date>p->next->date)
    53. {
    54.                 temp=p->date;
    55.                 p->date=p->next->date;
    56.                 p->next->date=temp;
    57. }
    58.             p=p->next;
    59. }
    60. }
    61. return head;
    62. }
    63. //链表插入
    64. node * insert(node *head,int num)
    65. {
    66.     node *p,*q,*t;
    67.     p=head;
    68.     t=new node();
    69.     t->date=num;
    70. while(num>p->date&&p->next!=NULL)
    71. {
    72.         q=p;
    73.         p=p->next;
    74. }
    75. if(num<=p->date)
    76. {
    77. if(p==head)
    78. {
    79.             t->next=p;
    80.             head=t;
    81. }
    82. else
    83. {
    84.             t->next=p;
    85.             q->next=t;
    86. }
    87. }
    88. else//把插入点定在表尾
    89. {
    90.         p->next=t;
    91.         t->next=NULL;
    92. }
    93. return head;
    94. }
    95. //链表输出
    96. void print(node *head)
    97. {
    98. struct node *p=head;
    99. while(p!=NULL)
    100. {
    101. printf("%d ",p->date);
    102.         p=p->next;
    103. }
    104. }
    105. //链表删除
    106. node *del(node *head,int num)
    107. {
    108.     node *p,*q;
    109.     p=head;
    110. while(num!=p->date&&p->next!=NULL)
    111. {
    112.         q=p;
    113.         p=p->next;
    114. }
    115. if(p->date==num)
    116. {
    117. if(p==head)
    118. {
    119.             head=p->next;
    120.             delete p;
    121. }
    122. else
    123. {
    124.             q->next=p->next;
    125.             delete p;
    126. }
    127. }
    128. else
    129. printf("Didn't Fond The Num!");
    130. return head;
    131. }
    132. //链表逆序
    133. node *reverse(node *head)
    134. {
    135.     node *p1,*p2,*p3;
    136.     p1=head;
    137.     p2=head->next;
    138. while(p2)
    139. {
    140.         p3=p2->next;
    141.         p2->next=p1;
    142.         p1=p2;
    143.         p2=p3;
    144. }
    145.     head->next=NULL;
    146.     head=p1;
    147. return head;
    148. }
    149. void main()
    150. {
    151. struct node *head;
    152. int n,m;
    153. printf("链表创建,输入数字: ");
    154.     head=creat();
    155.     print(head);
    156.     n=lenght(head);
    157. printf("链表的长度为 %d ",n);
    158. printf("链表排序: ");
    159.     head=sort(head,n);
    160.     print(head);
    161. printf("输入要插入链表的数: ");
    162. scanf("%d",&m);
    163. printf("插入后的链表: ");
    164.     head=insert(head,m);
    165.     print(head);
    166. printf("链表逆序: ");
    167.     head=reverse(head);
    168.     print(head);
    169. }
  • 相关阅读:
    Github创建远程库
    注册和登录Github
    Github简介
    一个成都程序猿写于离开北京一周年与26岁生日的这一天。
    【原创】面试时遇到『看门狗』脖子上挂着『时间轮』,我就问你怕不怕?
    【编程玄学】一个困扰我122天的技术问题,我好像知道答案了。
    【原创】(求锤得锤的故事)Redis锁从面试连环炮聊到神仙打架。
    【原创】面试官:你回去等通知吧!
    【原创】面试官问我G1回收器怎么知道你是什么时候的垃圾?
    【原创】面试官:你说你熟悉jvm?那你讲一下并发的可达性分析
  • 原文地址:https://www.cnblogs.com/AmatVictorialCuram/p/3435417.html
Copyright © 2020-2023  润新知