首页 > 代码库 > 单链表练习

单链表练习

  1 #include <stdio.h>  2 #include <stdlib.h>  3 struct person{  4         int age;  5         struct person *next;  6 };  7   8 struct person *insert(struct person *head, int age)  9 { 10         struct person *tmp = malloc(sizeof(struct person)); 11         tmp -> age = age; 12         tmp -> next = head; 13         return tmp; 14 } 15  16 void show(struct person *h) 17 { 18         while(1){ 19                 if(h == NULL){ 20                 //      printf("%d\n",h->age); 21                         break; 22                 } 23                 printf("age is %d\n", h->age); 24                 h = h->next; 25         } 26 } 27  28 struct person *insert_sec(struct person *h, int age) 29 { 30         struct person *tom = malloc(sizeof(struct person)); 31         tom->age = age; 32         struct person *tmp = h->next; 33         tom->next = tmp; 34         h->next = tom; 35  36         return h; 37 } 38  39 struct person *del(struct person *h, int age) 40 { 41  42         struct person *tmp = h; 43         while(1){ 44                 if(h->next->age == 23) 45                 { 46                         h->next = h->next->next; 47                         break; 48                 } 49                 h = h->next; 50         } 51         return tmp; 52 } 53  54 struct person *insert_tail(struct person *head, int age) 55 { 56         struct person *tmp = head; 57         struct person *tail = malloc(sizeof(struct person)); 58  59         while(1){ 60                 if(head->next == NULL) 61                         break; 62                 head = head -> next; 63         } 64         head -> next = tail; 65         tail -> age = age; 66         tail -> next = NULL; 67  68         return tmp; 69 } 70  71 struct person *find_min(struct person *head) 72 { 73         struct person *min = head; 74         struct person *tmp = head; 75  76         while(tmp){ 77                 if(tmp->next == NULL) 78                         break; 79                 if(min->age > tmp->age) 80                         min = tmp; 81                 tmp = tmp->next; 82         } 83         return min; 84 } 85  86 struct person * find_pro(struct person *head) 87 { 88         struct person *min_pro = head; 89         struct person *tmp = head; 90  91         if(min_pro->age != find_min(head)->age){ 92                 while(1){ 93                         if(min_pro->next->age == find_min(head)->age) 94                                 break; 95                         min_pro = min_pro -> next; 96                 } 97         } 98         return min_pro; 99 }100 #if 0101 struct person *sort(struct person *head)102 {103         struct person *tmp = head;104         struct person *tmp1 = head;105         int buffer = 0;106         for(tmp=head; tmp->next; tmp=tmp->next){107                 for(tmp1=tmp->next; tmp1; tmp1=tmp1->next){108                         if(tmp->age > tmp1->age){109                                 buffer = tmp->age;110                                 tmp->age = tmp1->age;111                                 tmp1->age = buffer;112                         }113                 }114         }115 116         return head;117 }118 #endif119 120 struct person *sort(struct person *head)121 {122         if((head == NULL) || (head->next == NULL))123                 return head;124 125         struct person *tmp = NULL;126         struct person *min = NULL;127         struct person *min_pre = NULL;128         struct person *newhead = NULL;129         struct person *oldhead = head;130 131         while(oldhead)132         {133                 //step 1:find min_pre134                 tmp = oldhead;135                 min = oldhead;136                 min_pre = NULL;137                 while(tmp->next != NULL){138                         if(min->age > tmp->next->age){139                                 min_pre = tmp;140                                 min = tmp->next;141                         }142                         tmp = tmp -> next;143                 }144 145                 //step 2:cat min146                 if(min == oldhead){147                         oldhead = min->next;148                         min->next = NULL;149                 }150                 else{151                         min_pre->next = min->next;152                         min->next = NULL;153                 }154                 //step 3:add newhead155                 if(newhead == NULL)156                         newhead = min;157                 else{158                         min->next = newhead;159                         newhead = min;160                 }161         }162 }163 int main()164 {165         struct person *tom = NULL;166         struct person *min = NULL;167         struct person *min_pro = NULL;168 169         tom = insert(tom, 21);170         tom = insert(tom, 23);171         tom = insert(tom, 22);172         tom = insert(tom, 24);173         tom = insert(tom, 29);174         tom = insert(tom, 21);175 176         show(tom);177         printf("\n");178         tom = del(tom, 23);179         show(tom);180 181         printf("\n");182         tom = insert_tail(tom, 26);183         show(tom);184 185         printf("\n");186         min = find_min(tom);187         printf("min is %d\n", min->age);188 189         min_pro = find_pro(tom);190         printf("min_pro is %d\n", min_pro->age);191 192         tom = sort(tom);193         show(tom);194 }195 

多练习就好!

单链表练习