基本算法实现小结(一)—— 单链表

各种基本算法实现小结(一)—— 单链表

(均已测试通过)

 

============================================================

单链表(测试通过)   

测试环境: Win-TC

[cpp:showcolumns] view plaincopy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #include <stdio.h>  
  2. struct _node  
  3. {  
  4.     int data;  
  5.     struct _node *next;  
  6. };  
  7. typedef struct _node list;  
  8. void display(list *l)  
  9. {  
  10.     list *p;  
  11.     p=l;  
  12.     while(p->next)  
  13.     {  
  14.         printf("%5d", p->next->data);  
  15.         p=p->next;  
  16.     }  
  17. }  
  18. void main()  
  19. {  
  20.     int i, n;  
  21.     list *h, *p, *s;  
  22.     printf("Enter num n:");  
  23.     scanf("%d", &n);  
  24.     h=(list*)malloc(sizeof(list));  
  25.     h->data=-1;  
  26.     h->next=NULL;  
  27.     s=p=h;  
  28.     for(i=n;i>0;i--)  
  29.     {  
  30.         p=(list*)malloc(sizeof(list));  
  31.         scanf("%d", &(p->data));  
  32.         p->next=h->next;  
  33.         h->next=p;  
  34.         h=h->next;  
  35.     }  
  36.     display(s);  
  37.     getch();  
  38. }  

 

运行结果:


 

=================================================

单链表各种操作(测试通过)   

测试环境: Win-TC

[cpp:showcolumns] view plaincopy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <stdlib.h>  
  4. struct _node  
  5. {  
  6.     int data;  
  7.     struct _node *next;  
  8. };  
  9. typedef struct _node node, *plist;  
  10. plist init_list()  
  11. {  
  12.     plist pl;  
  13.     pl=(plist)malloc(sizeof(node));  
  14.     if(NULL==pl)  
  15.     {  
  16.         printf("init list,  malloc is fail.../n");  
  17.         return NULL;  
  18.     }  
  19.     pl->data=-1;  
  20.     pl->next=NULL;  
  21.     return pl;  
  22. }  
  23. int isempty_list(plist pl)  
  24. {  
  25.     if(NULL==pl || NULL!=pl->next)  
  26.         return 1;  
  27.     else  
  28.         return 0;  
  29. }  
  30. plist clear_list(plist pl)  
  31. {  
  32.     pl=NULL;  
  33.     return pl;  
  34. }  
  35. void destroy_list(plist pl)  
  36. {  
  37.     plist p, s;  
  38.       
  39.     p=pl->next;  
  40.     while(p)  
  41.     {  
  42.         s=p;  
  43.         p=p->next;  
  44.         free(s);       
  45.     }  
  46.       
  47.     pl=NULL;  
  48. }  
  49. void insert_item(plist pl, int i, int e)  
  50. {  
  51.     int j=1;  
  52.     plist p, s;  
  53.     p=pl;  
  54.     while(p && j<i)  
  55.     {  
  56.         p=p->next;  
  57.         j++;  
  58.     }  
  59.     if(!p || j>i)  /* >len or <1  */  
  60.         printf("Insert fail.../n");  
  61.     s=(plist)malloc(sizeof(node));  
  62.     s->data=e;  
  63.     s->next=p->next;  
  64.     p->next=s;  
  65. }  
  66. void display(plist pl)  
  67. {  
  68.     plist p;  
  69.     p=pl->next;  
  70.     while(pl && p)  
  71.     {  
  72.         printf("%5d", p->data);  
  73.         p=p->next;  
  74.     }  
  75.     printf("/n/n");  
  76. }  
  77. int getbyid_item(plist pl, int i)  
  78. {  
  79.     plist p=pl->next;  
  80.     int j=1;  
  81.     while(p && j<i)  
  82.     {  
  83.         p=p->next;  
  84.         j++;  
  85.     }  
  86.     if(!p || j>i)  /* >len or <1  */  
  87.     {  
  88.         printf("fail.../n");  
  89.         exit(1);  
  90.     }  
  91.     return p->data;  
  92. }  
  93. int locate_item(plist pl, int e)  
  94. {  
  95.     plist p=pl->next;  
  96.     int j=1;  
  97.     while(p->data != e && p->next)  
  98.     {  
  99.         p=p->next;  
  100.         j++;  
  101.     }  
  102.     if(p->data == e)  
  103.         return j;  
  104.     else  
  105.     {  
  106.         printf("There is n %d in list/n", e);  
  107.         return -1;  
  108.     }  
  109. }  
  110. void delete_item(plist pl, int i, int *e)  
  111. {  
  112.     plist p=pl;  
  113.     plist q;  
  114.     int j=1;  
  115.     while(p->next && j<i)  
  116.     {  
  117.         p=p->next;  
  118.         j++;  
  119.     }  
  120.     if(!p->next || j>i)  /* >len or <1  */  
  121.     {  
  122.         printf("fail..../n");  
  123.         return;  
  124.     }  
  125.     q=p->next;  
  126.     p->next=q->next;  
  127.     *e=q->data;  
  128.     free(q);  
  129. }  
  130. int len_list(plist pl)  
  131. {  
  132.     int j=0;  
  133.     plist p=pl;  
  134.     while(pl && p->next)  
  135.     {  
  136.         j++;  
  137.         p=p->next;  
  138.     }  
  139.     return j;  
  140. }  
  141. plist traverse_list(plist pl)  
  142. {  
  143.     plist h, p, s;  
  144.       
  145.     if(!pl || !pl->next)  
  146.         return pl;  
  147.     h=pl->next;  
  148.     s=h;  
  149.     p=s->next;  
  150.     h->next=NULL;  
  151.     while(p)  
  152.     {  
  153.         s=p;  
  154.         p=p->next;  
  155.         s->next=h;  
  156.         h=s;  
  157.     }  
  158.     pl->next=h;  
  159.       
  160.     return pl;  
  161. }  
  162. void main()  
  163. {  
  164.     int len, pos, *del;  
  165.     plist pl=NULL;  
  166.     del=(int *)malloc(sizeof(int));  
  167.     pl=init_list();  
  168.     isempty_list(pl);  
  169.     insert_item(pl, 1, 1);  
  170.     insert_item(pl, 2, 3);  
  171.     insert_item(pl, 3, 5);  
  172.     insert_item(pl, 4, 7);  
  173.     insert_item(pl, 5, 9);  
  174.     insert_item(pl, 6, 11);  
  175.     display(pl);  
  176.     len=len_list(pl);  
  177.     printf("link list len: %d/n", len);  
  178.     pos=locate_item(pl, 7);  
  179.     printf("num 7 pos: %d/n", pos);  
  180.     delete_item(pl, 3, del);  
  181.     printf("delete pos 3 num: %d/n", *del);  
  182.     display(pl);  
  183.     printf("link list traverse.../n");  
  184.     pl=traverse_list(pl);  
  185.     display(pl);  
  186.     destroy_list(pl);  
  187.     getch();  
  188. }  

运行结果:


 

=================================================

单向循环链表(测试通过)   

测试环境: Win-TC

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. struct _node  
  4. {  
  5.     int data;  
  6.     struct _node *next;  
  7. };  
  8. typedef struct _node node, *plist;  
  9. plist init_list()  
  10. {  
  11.     plist pl=(plist)malloc(sizeof(node));  
  12.     if(!pl)  
  13.     {  
  14.         printf("error malloc fail.../n");  
  15.         return NULL;  
  16.     }  
  17.     pl->data=-1;  
  18.     pl->next=pl;    /* pl->next=NULL */  
  19.     return pl;  
  20. }  
  21. void insert_item(plist pl, int pos, int data)  
  22. {  
  23.     int j=0;  
  24.     plist p,s;  
  25.     s=p=pl;  
  26.     while(p && j<pos-1)  
  27.     {  
  28.         p=p->next;  
  29.         j++;  
  30.     }  
  31.     if(!p || j>pos-1)  
  32.     {  
  33.         printf("Error insert fail.../n");  
  34.         return;  
  35.     }  
  36.     s=(plist)malloc(sizeof(node));  
  37.     if(!s)  
  38.     {  
  39.         printf("Error malloc fail.../n");  
  40.         return;  
  41.     }  
  42.     s->data=data;  
  43.     s->next=p->next;  
  44.     p->next=s;  
  45. }  
  46. int find_item(plist pl, int data)  
  47. {  
  48.     plist s,p;  
  49.     s=p=pl;  
  50.     p=p->next;  
  51.     while(s != p)  
  52.     {  
  53.         if(data==p->data)  
  54.             return 1;  
  55.         p=p->next;  
  56.     }  
  57.     return 0;  
  58. }  
  59. void delete_item(plist pl, int data)  
  60. {  
  61.     plist p,s;  
  62.     s=p=pl;  
  63.     if(data == p->data) /* first item is equal with data, then last item = second item */  
  64.     {  
  65.         s=p;  
  66.         while(s != p->next)  
  67.             p=p->next;  
  68.         p->next=s->next;  
  69.         return;  
  70.     }  
  71.     while(s != p->next) /* first item is not equal with data */  
  72.     {  
  73.         if(data == p->next->data)  
  74.         {  
  75.             p->next=p->next->next;  
  76.             return;  
  77.         }  
  78.         p=p->next;  
  79.     }  
  80. }  
  81. void display(plist pl)  
  82. {  
  83.     plist s,p;  
  84.     s=p=pl;  
  85.     printf("%5d", p->data); /* print first item */  
  86.     p=p->next;  
  87.     while(s != p)  
  88.     {  
  89.         printf("%5d", p->data);  
  90.         p=p->next;  
  91.     }  
  92.     printf("/n/n");  
  93. }  
  94. void main()  
  95. {  
  96.     int f;  
  97.     plist pl;  
  98.     pl=init_list();  
  99.     insert_item(pl, 1, 1);  
  100.     insert_item(pl, 2, 3);  
  101.     insert_item(pl, 3, 5);  
  102.     insert_item(pl, 4, 7);  
  103.     insert_item(pl, 5, 9);  
  104.     display(pl);  
  105.     printf("Finding 3.../n");  
  106.     f=find_item(pl, 3);  
  107.     if(f)  
  108.         printf("True find 3/n");  
  109.     else  
  110.         printf("False find 3.../n");  
  111.     printf("/nDeleting 1.../n");  
  112.     delete_item(pl->next, 1);  
  113.     display(pl->next);  
  114.     getch();  
  115. }  

运行结果:

 
=================================================

双向循环链表(测试通过)   

测试环境: Win-TC

[cpp:showcolumns] view plaincopy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. struct _node  
  4. {  
  5.     int data;  
  6.     struct _node *prior;  
  7.     struct _node *next;  
  8. };  
  9. typedef struct _node node, *plist;  
  10. plist init_list()  
  11. {  
  12.     plist p;  
  13.     p=(plist)malloc(sizeof(node));  
  14.     if(!p)  
  15.     {  
  16.         printf("Error, malloc fail.../n");  
  17.         return NULL;  
  18.     }  
  19.     p->data=-1;   /* head->data = -1 */  
  20.     p->prior=p;  
  21.     p->next=p;  
  22.     return p;  
  23. }  
  24. void insert_item(plist pl, int pos, int data)  
  25. {  
  26.     int j=0;  
  27.     plist s,p;  
  28.     p=pl;  
  29.     while(p && j<pos-1)  
  30.     {  
  31.         p=p->next;  
  32.         j++;  
  33.     }  
  34.     if(!p || j>pos-1) /* pos is less than 1 or pos larger than len_list+1 */  
  35.     {  
  36.         printf("Error %d is invalide num.../n", pos);  
  37.         return;  
  38.     }  
  39.     s=(plist)malloc(sizeof(node));  
  40.     if(!s)  
  41.     {  
  42.         printf("Error, malloc fail.../n");  
  43.         return NULL;  
  44.     }  
  45.     s->data=data;  
  46.     s->prior=p;  
  47.     s->next=p->next;  
  48.     p->next->prior=s;  
  49.     p->next=s;  
  50. }  
  51. int find_item(plist pl, int data)  
  52. {  
  53.     plist s,p;  
  54.     s=p=pl;  
  55.     if(data == p->data)  
  56.         return 1;  
  57.     p=p->next;  
  58.     while(s != p)  
  59.     {  
  60.         if(data == p->data)  
  61.             return 1;  
  62.         p=p->next;  
  63.     }  
  64.     return 0;  
  65. }  
  66. void delete_item(plist pl, int data)  
  67. {  
  68.     plist s,p;  
  69.     s=p=pl;  
  70.     if(data == p->data) /* first check equal */  
  71.     {  
  72.         p->prior->next=p->next;  
  73.         p->next=p->prior;  
  74.         return;  
  75.     }  
  76.     while(s != p->next)  
  77.     {  
  78.         if(data == p->next->data)  
  79.         {  
  80.             p->next=p->next->next;  
  81.             p->next->next->prior=p;  
  82.         }  
  83.         p=p->next;  
  84.     }  
  85. }  
  86. void display(plist pl)  
  87. {  
  88.     plist s,p;  
  89.     s=p=pl;  
  90.     printf("%5d", p->data);  /* first item, such as head->data is -1 */  
  91.     p=p->next;  
  92.     while(s != p)  
  93.     {  
  94.         printf("%5d", p->data);  
  95.         p=p->next;  
  96.     }  
  97.     printf("/n/n");  
  98. }  
  99. void main()  
  100. {  
  101.     int f;  
  102.     plist pl;  
  103.     pl=init_list();  
  104.     insert_item(pl, 1, 1);  
  105.     insert_item(pl, 2, 3);  
  106.     insert_item(pl, 3, 5);  
  107.     insert_item(pl, 4, 7);  
  108.     insert_item(pl, 5, 9);  
  109.     display(pl);  
  110.     printf("Finding 3.../n");  
  111.     f=find_item(pl->next->next, 3);  
  112.     if(f)  
  113.         printf("True find 3/n");  
  114.     else  
  115.         printf("Fail find 3.../n");  
  116.     printf("Finding 6.../n");  
  117.     f=find_item(pl->prior->prior, 6);  
  118.     if(f)  
  119.         printf("True find 6/n");  
  120.     else  
  121.         printf("Fail find 6.../n");  
  122.     printf("/nDeleting 3.../n");  
  123.     delete_item(pl->next->next, 3);  
  124.     display(pl);  
  125.     getch();  
  126. }  
运行结果:
======================================================

以上代码,均在Win-TC 1.9.1 编译器环境测试通过(测试部分用例)

由于刚刚学习算法,编程水平有限,以上代码多有考虑不周或错误

欢迎大家在下面留言,改正或优化代码,给予指点或建议,谢谢!
原文地址:https://www.cnblogs.com/new0801/p/6175891.html