歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言實現單鏈表的操作:創建,刪除,插入,反轉

C語言實現單鏈表的操作:創建,刪除,插入,反轉

日期:2017/3/1 11:09:02   编辑:Linux編程

剛學了數據結構的單鏈表基本操作:創建,刪除,插入,反轉等,以下是詳細內容,其中很多關於數據結構的表述並不完整,只是簡單的基本算法思想的表現。

由於沒經驗,代碼有點亂·····

如有不當或錯誤之處,歡迎指正,不勝感激!

  1. #include<stdio.h>
  2. #include <malloc.h>
  3. struct node
  4. {
  5. int data;
  6. struct node* next;
  7. }head;
  8. struct node *p,*q;//聲明臨時節點
  9. void head_insert(int x)//從頭部插入新節點
  10. {
  11. p=(struct node*)malloc(sizeof(struct node));
  12. if(p==NULL)
  13. {
  14. printf("內存申請失敗,退出");
  15. exit(0);
  16. }
  17. p->data=x;
  18. p->next=head.next;
  19. head.next=p;
  20. }
  21. void tail_insert(int x)//從尾部插入節點
  22. {
  23. p=(struct node*)malloc(sizeof(struct node));
  24. if(p==NULL)
  25. {
  26. printf("內存申請失敗,退出");
  27. exit(0);
  28. }
  29. p->data=x;
  30. p->next=NULL;
  31. q->next=p;
  32. q=p;
  33. }
  34. void node_length()//輸出鏈表長度
  35. {
  36. int length=0;
  37. p=head.next;
  38. if(p==NULL) printf("The length of node is 0.\n");
  39. else
  40. {
  41. do
  42. {
  43. length++;
  44. p=p->next;
  45. } while(p);
  46. printf("The length of node is %d.\n",length);
  47. }
  48. }
  49. void print_node()//打印鏈表
  50. {
  51. printf("輸出此時鏈表:\n");
  52. p=head.next;
  53. if(p==NULL) {printf("NULL\n");return;}
  54. else
  55. {
  56. while(p)
  57. {
  58. printf("%d ",p->data);
  59. p=p->next;
  60. }
  61. printf("\n");
  62. }
  63. }
  64. void clear_node()//清空鏈表
  65. {
  66. p=head.next;
  67. head.next=NULL;
  68. while(p)
  69. {
  70. q=p;
  71. p=p->next;
  72. free(q);
  73. }
  74. }
  75. void new_insert(int i,int a)//在第i個位置後插入新整型元素 a
  76. {
  77. q=(struct node*)malloc(sizeof(struct node));
  78. q->data=a;
  79. p=head.next;
  80. if(i<0) printf("Position Error.\n");
  81. else
  82. {
  83. while(p&&--i) p=p->next;
  84. if(i) printf("Position Error.\n");
  85. else
  86. {
  87. q->next=p->next;
  88. p->next=q;
  89. }
  90. }
  91. }
  92. void delete_node(int i)//刪除某節點
  93. {
  94. p=&head;
  95. while(p->next&&--i)
  96. p=p->next;
  97. if(i) printf("Position Error.\n");
  98. else
  99. {
  100. q=p->next;
  101. p->next=q->next;
  102. free(q);
  103. }
  104. }
  105. void invert_order()//將鏈表反轉
  106. {
  107. node *This,*prev;
  108. p=head.next;
  109. This=NULL;
  110. while(p)
  111. {
  112. prev=This;
  113. This=p;
  114. p=p->next;
  115. This->next=prev;
  116. }
  117. head.next=This;
  118. }
  119. int main()
  120. {
  121. int number,i,a;
  122. head.next=NULL;
  123. q=&head;
  124. printf("輸入整型數據:\n");
  125. while(scanf("%d",&number)!=EOF) //將數據存入鏈表
  126. {
  127. head_insert(number);//從頭插入 即逆序插入
  128. /* tail_insert(number); 從尾端 插入即正序插入 */
  129. }
  130. invert_order();
  131. node_length();//輸出鏈表長度
  132. print_node();//輸出鏈表
  133. printf("在第i個位置後插入a,請輸入i和a:\n");
  134. scanf("%d%d",&i,&a);
  135. new_insert(i,a);
  136. print_node();
  137. printf("輸入要刪除的第i個結點:\n");
  138. scanf("%d",&i);
  139. delete_node(i);
  140. print_node();
  141. clear_node();
  142. return 0;
  143. }

附:在Windows下,輸入數據完畢後先按Enter鍵,再按Ctrl+Z鍵,最後按Enter鍵即可結束輸入;在Linux下,輸入完畢後按Ctrl+D鍵可結束輸入。

Copyright © Linux教程網 All Rights Reserved