现在的位置: 首页 > 综合 > 正文

链表的实现(查找,插入,删除)

2013年10月11日 ⁄ 综合 ⁄ 共 8521字 ⁄ 字号 评论关闭

#include <stdio.h>                                                                                                          
  2 #include <stdlib.h>                                                                                                         
  3 struct node;                                                                                                                
  4 typedef struct node *P;//意思是p 指向struct node的指针                                                                      
  5 typedef P list;//list 与 ptrtonode 同等意思,便于在下面的编程中理解                                                         
  6 typedef P position;//意思是表示自引用结构中便于理解position是指向当前自身的指针                                             
  7 list makelist(list L);//用于创造空链表,而其类型是list原因是makelist返回的是链表,而其类型是list                          
  8 int isempty(list L);//判断是否为空链表,如果为空则返回true                                                                  
  9 int islast(position P,list L);//P是指向当前结点的指针,并且它的类型是position                                               
 10 position find (int x,list L);//在L链表中查找元素x                                                                           
 11 void delete(int x,list L);                                                                                                  
 12 void insert(int x,list L,position P);//P代表在结点,而x是代表在链表L中P结点前面插入x                                        
 13 position findprevious(int x,list L);                                                                                        
 14 position header(list L);//不知道有什么用                                                                                    
 15 position first(list L);//同上                                                                                               
 16 position advance(position P);//同上                                                                                         
 17 int retrieve(position P);//retrieve 意思是检索,恢复,其他同上                                                              
 18 void printlist(list L);                                                                                                     
 19 struct node{                                                                                                                
 20   int data;                                                                                                                 
 21   position next;                                                                                                            
 22 };                                                                                                                          
 23                                                                                                                             
 24 void main(){                                                                                                                
 25   list L;                                                                                                                   
 26   int n;                                                                                                                    
 27   L = makelist(list L);                                                                                                     
 28   printf("0 represent the element you want to look for in the list\n");                                                     
 29   printf("1 represent the element you want to delete in the list\n");                                                       
 30   printf("2 represent the element you want to insert in the list\n");                                                       
 31  // scanf("%d",&n);                                                                                                         
 32     while (n == 0 || n == 1|| n == 2){                                                                                      
 33     prinf("please input choose:\n");                                                                                        
 34        scanf("%d",&n);                                                                                                      
 35       switch(n){                                                                                                            
 36         case '0':                                                                                                           
 37            printf("please input element you want to look for:\n");                                                          
 38                    int c;                                                                                                   
 39              position P;                                                                                                    
 40                scanf("%d",&c);                                                                                              
 41                 P= find(c,L);                                                                                               
 42                 printf("%s",P);                                                                                             
 43         case '1':                                                                                                           
 44           printf("please input element you want to delete:\n");                                                             
 45              int c;                                                                                                         
 46             scanf("%d",&c);                                                                                                 
 47           delete(c,L);                                                                                                      
 48        case '2':                                                                                                            
 49           printf("please input element you want to insert:\n");                                                             
 50             int c;
 
 51             position P;
 52              scanf("%d,%s",&c,P);
 53             insert(c,L,P);
 54        default:
 55          return "choosing error";
 56       }
 57     }
 58 }
 59
 60 void printlist(list L){
 61   position P;
 62   P = header(L);
 63   printf("the list is :\n");
 64   if(P != NULL)
 65   do {
 66     printf("%d--",P -> data);
 67     p = P -> next;
 68   }while(P != NULL);
 69 }
 70
 71 list makelist(list L){
 72    position P;
 73    P =L;
 74    P -> next = NULL;
 75    return P;
 76 }
 77
 78 int isempty(list L){//return true if empty
 79   return L-> next == NULL;
 80 }
 81
 82 int islast(position P,list L){//ruturn true if P is the last position in list L.parametre(参数,系数) L is unused in this implementation(履行,实现)
 83      return P -> next == NULL;//judge if the list is last ,if the list is null ,the return's value is true(1)
 84 }
 85
 86 position find (int x,list L){//look for the present node x
 87    position P;
 88    p = L -> next;
 89    while(P != NULL && P -> data != x)
 90         P = P -> next;
 91     return P;
 92 }
 93
 94 void delete (int x,list L){
 95   position P,tmpcell;
 96      p = findprevious(x,L);
 97     if(!islast(P,L)){
 98       tmpcell = P -> next;
 99       p -> next = tmpcell -> next;
100       free(tmpcell);

 51             position P;
 52              scanf("%d,%s",&c,P);
 53             insert(c,L,P);
 54        default:
 55          return "choosing error";
 56       }
 57     }
 58 }
 59
 60 void printlist(list L){
 61   position P;
 62   P = header(L);
 63   printf("the list is :\n");
 64   if(P != NULL)
 65   do {
 66     printf("%d--",P -> data);
 67     p = P -> next;
 68   }while(P != NULL);
 69 }
 70
 71 list makelist(list L){
 72    position P;
 73    P =L;
 74    P -> next = NULL;
 75    return P;
 76 }
 77
 78 int isempty(list L){//return true if empty
 79   return L-> next == NULL;
 80 }
 81
 82 int islast(position P,list L){//ruturn true if P is the last position in list L.parametre(参数,系数) L is unused in this implementation(履行,实现)
 83      return P -> next == NULL;//judge if the list is last ,if the list is null ,the return's value is true(1)
 84 }
 85
 86 position find (int x,list L){//look for the present node x
 87    position P;
 88    p = L -> next;
 89    while(P != NULL && P -> data != x)
 90         P = P -> next;
 91     return P;
 92 }
 93
 94 void delete (int x,list L){
 95   position P,tmpcell;
 96      p = findprevious(x,L);
 97     if(!islast(P,L)){
 98       tmpcell = P -> next;
 99       p -> next = tmpcell -> next;
100       free(tmpcell);

101       printlist(L);
102      }
103     else
104     printf("the list is null\n");
105 }
106
107 position findprevious(x,L){//look for the next node  x
108   position P;
109   P = L;
110   while(P -> next != NULL && P -> next -> data != x)
111       P = P -> next;
112     return P;
113 }
114
115 void insert(int x,list L,position P){
116   position tmpcell;
117   tmpcell = malloc(sizeof(struct node));
118     if(tmp == NULL)
119      printf("out of space");
120      tmpcell -> data = x;
121      tmpcell -> next = P -> next;
122      P -> next = tmpcell;
123      print(L);
124 }

抱歉!评论已关闭.