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

本文提供了单链表、单向循环链表及双向循环链表的基本操作实现代码,包括初始化、插入、查找、删除等,并在Win-TC环境下测试通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

转载地址:http://blog.youkuaiyun.com/sunboy_2050/article/details/5640326

 

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

(均已测试通过)

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

单链表(测试通过)   

测试环境: Win-TC

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

运行结果:


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

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

测试环境: Win-TC

 

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


运行结果:


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

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

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

运行结果:


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

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

测试环境: Win-TC

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值