当前位置:   article > 正文

带头结点的单链表的基本操作_头歌顺序递归遍历单链表

头歌顺序递归遍历单链表

带头结点的单链表的基本操作

带头结点的单链表的基本操作初始化、 插入(头插法,尾插法)、删除、逆置、合并(递归、非递归)、排序、遍历

  1. /*
  2. *
  3. * 带头结点的单链表的基本操作
  4. 初始化、 插入(头插法,尾插法)、删除、
  5. 逆置、合并(递归、非递归)、排序、遍历
  6. *
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. typedef struct node
  11. {
  12. int data;
  13. struct node *next;
  14. }Node, *Linklist;
  15. Linklist init()
  16. {
  17. Node *head;
  18. head = (Node *)malloc(sizeof(Node));
  19. if(NULL==head)
  20. {
  21. printf("malloc failed....\n");
  22. return head;
  23. }
  24. head->next = NULL;
  25. return head;
  26. }
  27. Linklist insert_head(Node *head, int num)
  28. {
  29. Node *p;
  30. p = (Node *)malloc(sizeof(Node));
  31. if(NULL==p)
  32. {
  33. printf("malloc failed...\n");
  34. return NULL;
  35. }
  36. p->data = num;
  37. p->next=head->next;
  38. head->next=p;
  39. return head;
  40. }
  41. Linklist insert_tail(Node *head, int num)
  42. {
  43. Node *p=NULL, *t=NULL;
  44. p = (Node *)malloc(sizeof(Node));
  45. if(NULL==p)
  46. {
  47. printf("malloc failed...\n");
  48. return NULL;
  49. }
  50. p->data = num;
  51. t=head;
  52. while(t->next!=NULL)
  53. {
  54. t = t->next;
  55. }
  56. t->next = p;
  57. p->next = NULL;
  58. return head;
  59. }
  60. Linklist delete(Node *head, int num)
  61. {
  62. Node *p=NULL, *t=NULL;
  63. if(NULL==head || NULL==head->next)
  64. {
  65. return head;
  66. }
  67. p=head;
  68. while(p->next!=NULL)
  69. {
  70. if(p->next->data==num)
  71. {
  72. // p->next = p->next->next;
  73. t = p->next;
  74. p->next = t->next;
  75. free(t);
  76. }
  77. p = p->next;
  78. }
  79. return head;
  80. }
  81. Linklist reverse(Node *head)
  82. {
  83. Node *p=NULL, *q=NULL;
  84. if(NULL==head || NULL==head->next)
  85. {
  86. return head;
  87. }
  88. p = head->next;
  89. q = p->next; //q=head->next->next;
  90. p->next = NULL;
  91. while(q!=NULL)
  92. {
  93. p = q->next;
  94. q->next = head->next;
  95. head->next = q;
  96. q = p;
  97. }
  98. return head;
  99. }
  100. Linklist merge(Node *head1, Node *head2)
  101. {
  102. if(NULL==head1)
  103. return head2;
  104. if(NULL==head2)
  105. return head1;
  106. Node *head = NULL;
  107. if(head1->data < head2->data)
  108. {
  109. head = head1;
  110. head->next = merge(head1->next, head2);
  111. }
  112. else
  113. {
  114. head = head2;
  115. head->next = merge(head1, head2->next);
  116. }
  117. return head;
  118. }
  119. Linklist merge_list(Node *head1, Node*head2)
  120. {
  121. if(NULL==head1)
  122. return head2;
  123. if(NULL==head2)
  124. return head1;
  125. Node *head = NULL, *p=NULL, *q=NULL;
  126. if(head1->data < head2->data)
  127. {
  128. head = head1;
  129. p = head1->next;
  130. q = head2;
  131. }
  132. else
  133. {
  134. head = head2;
  135. q = head2->next;
  136. p = head1;
  137. }
  138. Node *cur = head;
  139. while(p!=NULL && q!=NULL)
  140. {
  141. if(p->data <= q->data)
  142. {
  143. cur->next = p;
  144. cur = p;
  145. p = p->next;
  146. }
  147. else
  148. {
  149. cur->next = q;
  150. cur = q;
  151. q = q->next;
  152. }
  153. }
  154. if(p!=NULL)
  155. {
  156. cur->next = p;
  157. }
  158. if(q!=NULL)
  159. {
  160. cur->next = q;
  161. }
  162. return head;
  163. }
  164. Linklist sort(Node *head)
  165. {
  166. Node *p = NULL, *q = NULL;
  167. int i=0, j=0, t=0;
  168. int count = 0;
  169. if(NULL==head || NULL==head->next)
  170. {
  171. return head;
  172. }
  173. p = head->next;
  174. while(p!=NULL)
  175. {
  176. count++;
  177. p = p->next;
  178. }
  179. for(i=0; i<count-1; i++)
  180. {
  181. p = head->next;
  182. q = p->next;
  183. for(j=0; j<count-1-i; j++)
  184. {
  185. if(p->data > q->data)
  186. {
  187. t = p->data;
  188. p->data = q->data;
  189. q->data = t;
  190. }
  191. p = p->next;
  192. q = q->next;
  193. }
  194. }
  195. return head;
  196. }
  197. Linklist traverse(Node *head)
  198. {
  199. Node *p=NULL;
  200. if(NULL==head || NULL==head->next)
  201. {
  202. return head;
  203. }
  204. p=head->next;
  205. while(p!=NULL)
  206. {
  207. printf(" %d", p->data);
  208. p = p->next;
  209. }
  210. return head;
  211. }
  212. int main()
  213. {
  214. Node *head1, *head2, *t;
  215. int i=0;
  216. head1 = init();
  217. for(i=0; i<10; i=i+2)
  218. insert_tail(head1, i);
  219. traverse(head1);
  220. printf("\n");
  221. head2 = init();
  222. for(i=0; i<10; i=i+2)
  223. insert_head(head2, i);
  224. traverse(head2);
  225. printf("\n");
  226. sort(head2);
  227. traverse(head2);
  228. printf("\n");
  229. t = merge(head1->next, head2);
  230. //t = merge_list(head1->next, head2);
  231. traverse(t);
  232. printf("\n");
  233. delete(t, 8);
  234. traverse(t);
  235. printf("\n");
  236. return 0;
  237. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/549108
推荐阅读
  

闽ICP备14008679号