1. 程式人生 > >單鏈表很全的例子,插入,刪除,,查詢,排序

單鏈表很全的例子,插入,刪除,,查詢,排序

單鏈表很全的例子,增加,刪除,排序,都有了

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedefstruct node  
  4. {  
  5. int nDate;  
  6. struct node *pstnext;  
  7. }Node;  
  8. //連結串列輸出
  9. void output(Node *head)  
  10. {  
  11. Node *p = head->pstnext;  
  12. while(NULL != p)  
  13. {  
  14.   printf("%d  ", p->nDate);   
  15.   p = p->pstnext;  
  16. }  
  17. printf("\r\n");  
  18. }  
  19. //連結串列建立
  20. Node* creat()  
  21. {  
  22. Node *head = NULL, *p = NULL, *s = NULL;  
  23. int Date = 0, cycle = 1;  
  24. head = (Node*)malloc(sizeof(Node));  
  25. if(NULL == head)  
  26. {  
  27.   printf("分配記憶體失敗\r\n");  
  28. return NULL;  
  29. }  
  30. head->pstnext = NULL;  
  31. p = head;  
  32. while(cycle)  
  33. {  
  34.   printf("請輸入資料且當輸入資料為0時結束輸入\r\n");  
  35.   scanf("%d", &Date);  
  36. if(0 != Date)  
  37.   {  
  38.    s = (Node*)malloc(sizeof(Node));  
  39. if(NULL == s)  
  40.    {  
  41.     printf("分配記憶體失敗\r\n");  
  42. return NULL;  
  43.    }  
  44.    s->nDate = Date;  
  45.    p->pstnext = s;  
  46.    p = s;  
  47.   }  
  48. else
  49.   {  
  50.    cycle = 0;  
  51.   }  
  52. }  
  53. p->pstnext = NULL;  
  54. return(head);  
  55. }  
  56. //單鏈表測長
  57. void length(Node *head)  
  58. {  
  59. Node *p = head->pstnext;  
  60. int j=0;  
  61. while(NULL != p)  
  62. {  
  63.   p = p->pstnext;  
  64.   j++;  
  65. }  
  66. printf("%d\r\n", j);  
  67. }  
  68. //連結串列按值查詢
  69. void research_Date(Node *head, int date)  
  70. {  
  71. Node *p;  
  72. int n=1;  
  73. p = head->pstnext;  
  74. while(NULL != p && date != p->nDate)  
  75. {  
  76.   p = p->pstnext;  
  77.   ++n;  
  78. }  
  79. if(NULL == p)  
  80. {  
  81.   printf("連結串列中沒有找到該值");  
  82. }elseif(date == p->nDate)  
  83. {  
  84.   printf("要查詢的值%d在連結串列中第%d個位置\r\n", date, n);  
  85. }  
  86. return;  
  87. }  
  88. //按序號查詢
  89. void research_Number(Node *head, int Num)  
  90. {  
  91. Node *p=head;  
  92. int i = 0;  
  93. while(NULL != p && i < Num)  
  94. {  
  95.   p = p->pstnext;  
  96.   i++;  
  97. }  
  98. if(p == NULL)  
  99. {  
  100.   printf("查詢位置不合法\r\n");  
  101. }elseif(i == 0)  
  102. {  
  103.   printf("查詢位置為頭結點\r\n");  
  104. }elseif(i == Num)  
  105. {  
  106.   printf("第%d個位置資料為%d\r\n", i, p->nDate);  
  107. }  
  108. }  
  109. //在指定元素之前插入新結點
  110. void insert_1(Node *head, int i, int Newdate)  
  111. {  
  112. Node *pre = head, *New = NULL;  
  113. int j = 0;  
  114. while(NULL != pre && j < i-1)  
  115. {   
  116.   pre = pre->pstnext;  
  117.   j++;  
  118. }  
  119. if(NULL == pre || j > i-1)  
  120. {  
  121.   printf("插入位置不存在\r\n");  
  122. }else
  123. {  
  124.   New = (Node*)malloc(sizeof(Node));  
  125. if(NULL == New)  
  126.   {  
  127.    printf("分配記憶體失敗\r\n");  
  128. return;  
  129.   }  
  130.   New->nDate = Newdate;  
  131.   New->pstnext = pre->pstnext;  
  132.   pre->pstnext = New;  
  133. }  
  134. }  
  135. //在指定元素之後插入新結點
  136. void insert_2(Node *head, int i, int Newdate)  
  137. {  
  138. Node *pre = head, *New = NULL;  
  139. int j = 0;  
  140. while(NULL != pre->pstnext && j < i)  
  141. {  
  142.   pre = pre->pstnext;  
  143.   j++;  
  144. }  
  145. if(j == i)  
  146. {  
  147.   New = (Node*)malloc(sizeof(Node));  
  148. if(NULL == New)  
  149.   {  
  150.    printf("分配記憶體失敗\r\n");  
  151. return;  
  152.   }  
  153.   New->nDate = Newdate;  
  154.   New->pstnext = pre->pstnext;  
  155.   pre->pstnext = New;  
  156. }else
  157. {  
  158.   printf("插入位置不存在\r\n");  
  159. }  
  160. }  
  161. //刪除指定結點
  162. void Delete_1(Node *head, int i3)  
  163. {  
  164. Node *p = head, *pre = NULL;  
  165. int j = 0;  
  166. while(NULL != p && j < i3)  
  167. {  
  168.   pre = p;  
  169.   p = p->pstnext;  
  170.   j++;  
  171. }  
  172. if(NULL == p)  
  173. {  
  174.   printf("刪除位置不存在\r\n");  
  175. }else
  176. {  
  177.   pre->pstnext = p->pstnext;  
  178.   free(p);  
  179. }  
  180. }  
  181. //指定刪除單鏈表中某個資料,並統計刪除此資料的個數
  182. int Delete_2(Node *head, int Delete_date)  
  183. {  
  184. int count = 0;  
  185. Node *p = head, *q;  
  186. while(NULL != p->pstnext)  
  187. {  
  188.   q = p->pstnext;  
  189. if(q->nDate == Delete_date)  
  190.   {  
  191.    p->pstnext = q->pstnext;  
  192.    free(q);  
  193.    ++count;  
  194.   }  
  195. else
  196.   {  
  197.    p = q;  
  198.   }  
  199. }  
  200. return count;  
  201. }  
  202. //連結串列逆置
  203. void Reverse_list(Node *head)  
  204. {  
  205. Node *q, *s;  
  206. if(NULL == head->pstnext || NULL == head->pstnext->pstnext)  
  207. {  
  208. return;  
  209. }  
  210. q = head->pstnext->pstnext;  
  211. head->pstnext->pstnext = NULL;  
  212. while(NULL != q)  
  213. {  
  214.   s = q->pstnext;  
  215.   q->pstnext = head->pstnext;  
  216.   head->pstnext = q;  
  217.   q = s;  
  218. }  
  219. }  
  220. //單鏈表的連線
  221. void connect_list(Node *head, Node *head_New)  
  222. {  
  223. Node *p = head;  
  224. while(NULL != p->pstnext)  
  225. {  
  226.   p = p->pstnext;  
  227. }  
  228. p->pstnext = head_New->pstnext;  
  229. }  
  230. //單鏈表銷燬
  231. void destroy_list(Node* head)  
  232. {  
  233. while (NULL != head)  
  234. {  
  235.   Node* temp = head;  
  236.   head = head->pstnext;  
  237.   free(temp);  
  238. }  
  239. }  
  240. main()  
  241. {  
  242. int date, num;    //待查詢資料
  243. int i3;     //指定刪除元素的位置
  244. int i1, i2, Newdate_1, Newdate_2;    //待插入的新資料
  245. int Delete_date, k;    //待刪除的資料與其個數
  246. Node *Head = NULL;   //定義頭結點
  247. Node *Head_New = NULL;  
  248. //連結串列建立
  249. Head = creat();  
  250. printf("輸出建立的單鏈表\r\n");  
  251. output(Head);  
  252. //單鏈表測長
  253. printf("單鏈表長度為\r\n");  
  254. length(Head);  
  255. //連結串列按值查詢
  256. printf("請輸入待查詢的資料\r\n");  
  257. scanf("%d", &date);  
  258.     research_Date(Head, date);  
  259. //連結串列按序號查詢
  260. printf("請輸入待查詢序號\r\n");  
  261. scanf("%d", &num);  
  262. research_Number(Head, num);  
  263. //在指定第i1個元素之前插入新元素Newdate
  264. printf("在指定第i個元素之前插入新元素Newdate");  
  265. printf("請輸入i與元素且以逗號間隔\r\n");  
  266. scanf("%d,%d", &i1, &Newdate_1);  
  267. insert_1(Head, i1, Newdate_1);  
  268. printf("插入後新連結串列\r\n");  
  269. output(Head);   
  270. //在指定第i2個元素之後插入新元素Newdate
  271. printf("在指定第i個元素之後插入新元素Newdate");  
  272. printf("請輸入i與元素且以逗號間隔\r\n");  
  273. scanf("%d,%d", &i2, &Newdate_2);  
  274. insert_2(Head, i2, Newdate_2);  
  275. printf("插入後新連結串列\r\n");  
  276. output(Head);   
  277. //指定刪除i3元素
  278. printf("刪除元素的位置\r\n");  
  279. scanf("%d", &i3);  
  280. Delete_1(Head, i3);  
  281. printf("刪除後新連結串列\r\n");  
  282. output(Head);  
  283. //指定刪除單鏈表中某個資料,並統計刪除此資料的個數
  284. printf("請輸入待刪除的元素\r\n");  
  285. scanf("%d", &Delete_date);  
  286. k = Delete_2(Head, Delete_date);  
  287. printf("刪除後新連結串列\r\n");  
  288. output(Head);  
  289. printf("刪除指定元素在連結串列中的個數為:");  
  290. printf("%d\r\n", k);  
  291. //單鏈表逆置
  292. Reverse_list(Head);  
  293. printf("逆置後輸出\r\n");  
  294. output(Head);  
  295. //單鏈表的連線
  296. printf("建立一個新連結串列\r\n");  
  297. Head_New = creat();  
  298. printf("輸出新連結串列");  
  299. output(Head);  
  300. printf("將新連結串列連線到原來連結串列的尾部並輸出\r\n");  
  301. connect_list(Head, Head_New);  
  302. output(Head);  
  303.          destroy_list(Head);  
  304. }  
  305. 下面是輸出結果:  


 VS2010下除錯結果