[关闭]
@Jayfeather 2018-05-18T07:58:30.000000Z 字数 4770 阅读 998

链表功能的实现

数据结构与算法分析


  1. // ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
  2. /************************************************************************/
  3. /* 以下是关于线性表链接存储(单链表)操作的18种算法 */
  4. /* 1.初始化线性表,即置单链表的表头指针为空 */
  5. /* 2.创建线性表,此函数输入负数终止读取数据*/
  6. /* 3.打印链表,链表的遍历*/
  7. /* 4.清除线性表L中的所有元素,即释放单链表L中所有的结点,使之成为一个空表 */
  8. /* 5.返回单链表的长度 */
  9. /* 6.检查单链表是否为空,若为空则返回1,否则返回0 */
  10. /* 7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行 */
  11. /* 8.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL */
  12. /* 9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0 */
  13. /* 10.向单链表的表头插入一个元素 */
  14. /* 11.向单链表的末尾添加一个元素 */
  15. /* 12.向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0 */
  16. /* 13.向有序单链表中插入元素x结点,使得插入后仍然有序 */
  17. /* 14.从单链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行 */
  18. /* 15.从单链表中删除表尾结点并返回它的值,若删除失败则停止程序运行 */
  19. /* 16.从单链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行 */
  20. /* 17.从单链表中删除值为x的第一个结点,若删除成功则返回1,否则返回0 */
  21. /* 18.交换2个元素的位置 */
  22. /* 19.将线性表进行快速排序 */
  23. /*其中19,18, 13,8因为时间关系还没有实现*/
  24. /************************************************************************/
  25. #include<stdio.h>
  26. #include "stdafx.h"
  27. #include<stdlib.h>
  28. typedef int elementtype;
  29. typedef struct node *ptrtonode;
  30. typedef ptrtonode list;
  31. typedef ptrtonode position;
  32. typedef struct node{
  33. elementtype element;
  34. position next;
  35. }node;
  36. node* NewLinkedlist(void);/* 2.创建线性表,此函数输入负数终止读取数据*/
  37. void FreeLinkedlist(node *);/* 4.清除线性表L中的所有元素,即释放单链表L中所有的结点,使之成为一个空表 */
  38. void PrintfLinkedlist(node *);/* 3.打印链表,链表的遍历*/
  39. int CountLinkedlist(node *);/* 5.返回单链表的长度 */
  40. int EmptyLinkedlist(node *);/* 6.检查单链表是否为空,若为空则返回1,否则返回0 */
  41. int CheckLinkedlist(node *);/* 7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行 */
  42. node* InsertLinkedlist(node *);/*10-12.向单项链表中第pos个节点插入元素,pos=0则从表头插入,pos>0则从表中插入 */
  43. int ChangeLinkedlist(node*);/* 9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0 */
  44. node* DeletLinkedlist(node*);/*14-16 删除链表的第pos个节点元素,返回链表表头的地址*/
  45. int main()
  46. {
  47. node *p1;
  48. p1=NewLinkedlist();/* 2.创建线性表,此函数输入负数终止读取数据*/
  49. EmptyLinkedlist(p1);/* 6.检查单链表是否为空,若为空则返回1,否则返回0 */
  50. PrintfLinkedlist(p1);/* 3.打印链表,链表的遍历*/
  51. CountLinkedlist(p1);/* 5.返回单链表的长度 */
  52. CheckLinkedlist(p1);/* 7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行 */
  53. p1=InsertLinkedlist(p1);/*8-10.向单项链表中第pos个节点插入元素,pos=0则从表头插入,pos>0则从表中插入 */
  54. p1 = DeletLinkedlist(p1);/*14-16 删除链表的第pos个节点元素,返回链表表头的地址*/
  55. ChangeLinkedlist(p1);/* 9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0 */
  56. FreeLinkedlist(p1);/* 4.清除线性表L中的所有元素,即释放单链表L中所有的结点,使之成为一个空表 */
  57. while(getchar()=='\n');
  58. getchar();
  59. return 0;
  60. }
  61. node* NewLinkedlist(void)/* 2.创建线性表,此函数输入负数终止读取数据*/
  62. {
  63. node *p1, *p2,*p3;
  64. p1 = p2 =p3= (node*)malloc(sizeof(node));
  65. if (p1 == NULL)
  66. printf("Fail to get the memory!\n");
  67. while (p1 != NULL)
  68. {
  69. scanf_s("%d", &p1->element);
  70. if (p1->element<0)
  71. {
  72. printf("Over\n");
  73. p2->next = NULL;
  74. break;
  75. }
  76. else
  77. {
  78. p2 = p1;
  79. p1 = p1->next = ((node*)malloc(sizeof(node)));
  80. }
  81. if (p1 == NULL)
  82. printf("Fail to get the memory!\n");
  83. }
  84. return p3;
  85. }
  86. void PrintfLinkedlist(node *p1)/* 3.打印链表,链表的遍历*/
  87. {
  88. while (p1->next != NULL)
  89. {
  90. printf("%d | ", p1->element);
  91. p1 = p1->next;
  92. }
  93. if(p1->element>0)
  94. printf("%d |\n", p1->element);
  95. printf("Complete!\n");
  96. }
  97. void FreeLinkedlist(node *p1)/* 4.清除线性表L中的所有元素,即释放单链表L中所有的结点,使之成为一个空表 */
  98. {
  99. node *p2 = p1;
  100. while (p2 != NULL )
  101. {
  102. p2 = p2->next;
  103. free(p1);
  104. p1 = p2;
  105. }
  106. free(p2);
  107. printf("Clear!\n");
  108. }
  109. int CountLinkedlist(node *p1)/* 5.返回单链表的长度 */
  110. {
  111. int n = 0;
  112. while (p1 != NULL && p1->element>0)
  113. {
  114. n++;
  115. p1 = p1->next;
  116. }
  117. printf("The Linkedlist has %d nodes\n", n);
  118. return n;
  119. }
  120. int EmptyLinkedlist(node *p1)/* 6.检查单链表是否为空,若为空则返回1,否则返回0 */
  121. {
  122. if (p1->next == NULL && p1->element<0)
  123. {
  124. printf("This is an empty linkedlist\n");
  125. return 0;
  126. }
  127. else
  128. {
  129. printf("This is not an empty linkedlist\n");
  130. return 1;
  131. }
  132. }
  133. int CheckLinkedlist(node *p1)/* 7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行 */
  134. {
  135. int n,i=0;
  136. node *p2=p1;
  137. printf("Please insert the node you want to check:");
  138. scanf_s("%d", &n);
  139. while (i < n &&p2!=NULL)
  140. {
  141. i++;
  142. p1 = p2;
  143. p2 = p2->next;
  144. }
  145. if (i == n)
  146. printf("The %d node is %d.\n", n, p1->element);
  147. else
  148. printf("Out of the range of your linkedlist\n");
  149. return p1 -> element;
  150. }
  151. node* InsertLinkedlist(node* p1)/*10-12.向单项链表中第pos个节点插入元素,pos=0则从表头插入,pos>0则从表中插入 */
  152. {
  153. int pos, n,i=1;
  154. node *p2,*p3;
  155. p3 = p1;
  156. printf("Please insert the pos of the node:");
  157. scanf_s("%d",&pos);
  158. printf("Please insert the element of the node:");
  159. scanf_s("%d", &n);
  160. p2 = (node*)malloc(sizeof(node));
  161. p2->element = n;
  162. p2->next = NULL;
  163. if (pos == 0)
  164. {
  165. p2->next = p1;
  166. }
  167. else
  168. {
  169. while (i < pos && p1->next != NULL)
  170. {
  171. i++;
  172. p1 = p1->next;
  173. }
  174. if (i == pos)
  175. {
  176. if (p1->next != NULL)
  177. p2->next = p1->next->next;
  178. p1->next = p2;
  179. }
  180. else
  181. printf("Out of the range of your linkedlist\n");
  182. p2 = p3;
  183. }
  184. PrintfLinkedlist(p2);
  185. return p2;
  186. }
  187. int ChangeLinkedlist(node *p1)/* 9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0 */
  188. {
  189. int pos, n, i = 1;
  190. node *p3;
  191. p3 = p1;
  192. printf("Please insert the pos of the node:");
  193. scanf_s("%d", &pos);
  194. printf("Please insert the element of the node:");
  195. scanf_s("%d", &n);
  196. while (i < pos && p1->next != NULL)
  197. {
  198. i++;
  199. p1 = p1->next;
  200. }
  201. if (i == pos)
  202. {
  203. p1->element = n;
  204. }
  205. else
  206. {
  207. printf("Out of the range of your linkedlist\n");
  208. return 0;
  209. }
  210. PrintfLinkedlist(p3);
  211. return 1;
  212. }
  213. node* DeletLinkedlist(node *p1)/*14-16 删除链表的第pos个节点元素,返回链表表头的地址*/
  214. {
  215. int pos, i = 2;
  216. node *p2,*p3;
  217. p3=p2 = p1;
  218. printf("Please insert the pos of the node you want to delet:");
  219. scanf_s("%d", &pos);
  220. if (pos == 1)
  221. {
  222. p1 = p1->next;
  223. free(p1);
  224. }
  225. while (i < pos && p1->next != NULL)
  226. {
  227. i++;
  228. p1 = p1->next;
  229. }
  230. if (i == pos)
  231. {
  232. p2 = p1->next;
  233. if (p1->next->next != NULL)
  234. p1->next = p1->next->next;
  235. else
  236. p1->next = NULL;
  237. free(p2);
  238. }
  239. else
  240. {
  241. printf("Out of the range of your linkedlist\n");
  242. return 0;
  243. }
  244. PrintfLinkedlist(p3);
  245. return p3;
  246. }

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注