首页 > 代码库 > 数据结构 链表

数据结构 链表

 具有基本的增删改查功能的链表

打了一中午 可算完事了……

  1 #include<stdio.h>  2 #include<string.h>  3 struct _list{  4     int value;  5     _list *next;  6     _list(){  7         value=http://www.mamicode.com/0;  8         next=NULL;  9     } 10 }; 11  12 int _size(_list *&node){ 13     if(node->next==NULL) return 0; 14     _list *used=node->next; 15     int sum=1; 16     while(true){ 17         if(used->next==NULL) break; 18         else{ 19             sum++; 20             used=used->next; 21         } 22     } 23     return sum; 24 } 25  26 void add(_list *&node,int value){ 27     if(node==NULL){ 28         node=new _list(); 29         node->value=http://www.mamicode.com/value; 30         node->next=NULL; 31     } 32     else add(node->next,value); 33 } 34  35 _list *_find(_list *&node,int local){ 36     _list *used=node->next; 37     int l=1; 38     while(l<local){ 39         used=used->next; 40         l++; 41     } 42     return used; 43 } 44  45 int find_value(_list *&node,int value){ 46     if(node->value=http://www.mamicode.com/=value) return 1; 47     _list *used=node->next; 48     int l=1; 49     while(l<_size(node)){ 50         if(used->value=http://www.mamicode.com/=value) break; 51         used=used->next; 52         l++; 53     } 54     return l; 55 } 56  57 void _insert(_list *&node,int local,int value){ 58     _list *used=_find(node,local-1); 59     _list *__list=new _list(); 60     __list->next=_find(node,local)->next; 61     __list->value=http://www.mamicode.com/value; 62     used->next=__list; 63 } 64  65 void change(_list *&node,int local,int value){ 66     _list *used=_find(node,local); 67     used->value=http://www.mamicode.com/value; 68 } 69  70 void _delete(_list *&node,int local){ 71     _list *_front=_find(node,local-1); 72     _list *now=_find(node,local); 73     _front->next=now->next; 74     delete(now); 75 } 76  77 void print(_list *&node){ 78     if(node->next==NULL){ 79         printf("default\n"); 80         return ; 81     } 82     _list *used=node->next; 83     printf("%d",used->value); 84     used=used->next; 85     while(used!=NULL){ 86         printf(" %d",used->value); 87         used=used->next; 88     } 89     printf("\n"); 90     return ; 91 } 92  93 int main(){ 94     char act[20]; 95     _list *node=new _list(); 96     while(scanf("%s",act)!=EOF){ 97         if(strcmp(act,"end")==0) break; 98         else if(strcmp(act,"print")==0) print(node); 99         else if(strcmp(act,"add")==0){100             int value;101             scanf("%d",&value);102             add(node,value);103         }104         else if(strcmp(act,"delete")==0){105             int local;106             scanf("%d",&local);107             _delete(node,local);108         }109         else if(strcmp(act,"size")==0){110             printf("%d\n",_size(node));111         }112         else if(strcmp(act,"Insert")==0){113             int local,value;114             scanf("%d%d",&local,&value);115             _insert(node,local,value);116         }117         else if(strcmp(act,"change")==0){118             int local,value;119             scanf("%d%d",&local,&value);120             change(node,local,value);121         }122         else if(strcmp(act,"find")==0){123             int local;124             scanf("%d",&local);125             if(local>_size(node)) printf("default\n");126             else printf("%d\n",_find(node,local)->value);127         }128         else if(strcmp(act,"find_value")==0){129             int local;130             scanf("%d",&local);131             if(local>_size(node)) printf("default\n");132             else if(find_value(node,local)==-1) printf("none\n");133             else printf("%d\n",find_value(node,local));134         }135     }136     return 0;137 }138 /*139 140 add 1141 add 2142 add 3143 add 4144 print145 delete 2146 print147 find_value 3148 find 3149 change 2 100150 print151 size152 end153 154 */

 

数据结构 链表