单链表很全的例子,插入,删除,,查找,排序

来源:互联网 发布:阿里云备案买服务器 编辑:程序博客网 时间:2024/05/24 07:33

 

单链表功能大全

分类: C基础 9500人阅读 评论(6)收藏 举报

单链表很全的例子,增加,删除,排序,都有了

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


 VS2010下调试结果