链表的动态输入、插入 、与删除

来源:互联网 发布:区网络信息管理中心 编辑:程序博客网 时间:2024/05/16 23:53
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 
  5 #define len sizeof(struct student)
  6 
  7 
  8 struct student
  9 {
 10     int num;
 11     float score;
 12     struct student *next;
 13 };
 14 
 15 typedef struct student stu;
 16 stu *input(stu *head,stu *p,stu *tail);
 17 stu *insert(stu *head, stu *indata, int n);
 18 stu *delite(stu*head,int m);
 19 void print(stu *head);
 20 
 21 
 22 int main()
 23 {
 24     stu *head;
 25     stu *p, *tail;
 26     stu *indata;
 27     int m, n;
 28 
 29     head=input(head,p,tail);
 30     print(head);
 31 
 32     indata=(stu *)malloc(len);
     33     printf("input cha ru de weizhi\n");
 34     scanf("%d",&n);
 35     printf("input yao cha ru de lianbiao\n");
 36     scanf("%d %f",&indata->num,&indata->score);
 37     head=insert(head,indata,n);
 38 
 39     print(head);
 40 
 41     printf("input yao shan chu de lianbiao\n");
 42     scanf("%d",&m);
 43     head=delite(head, m);
 44     print(head);
 45 
 46     return 0;
 47 }
 48 
 49 stu *input(stu *head, stu *p, stu *tail)
 50 {
 51     int count=0;
 52 
 53 
 54     while(tail!=NULL&&count<4)
 55     {
 56         tail=(stu *)malloc(len);
 57         scanf("%d %f",&tail->num, &tail->score);
 58         count++;
 59         if(count==1)
 60             head=tail;
 61         else
 62             p->next=tail;
 63         p=tail;
 64 //      printf("%d %f\n",p->num,p->score);
  65         if(count==3)
 66             tail=NULL;
 67 //      print(head);    
 68     }
 69 
 70     return head;
 71 }
 72 
 73 stu *insert(stu *head, stu *indata, int n)
 74 {
 75     stu *look;
 76     int count=0;
 77     if(head==NULL)
 78     {
 79         head=indata;
 80         indata->next=NULL;
 81     }
 82     else
 83     {
 84         if(n==0)
 85         {
 86             indata->next=head;
 87             head=indata;
 88         }
 89         else
 90         {
 91             look=head;
 92             while((look!=NULL)&&(count<n-1))
 93             {
 94                 look=look->next;
 95                 count++;
 96             }
  95                 count++;
 96             }
 97             if(count==n-1)
 98             {
 99                 indata->next=look->next;
100                 look->next=indata;
101             }
102             if(indata==NULL)
103             {
104                 printf("out of range\n");
105             }
106         }
107 
108     }
109     return (head);
110 }
111 
112 
113 stu *delite(stu*head,int m)
114 {
115     stu *present, *after;
116 
117     if(head==NULL)
118     {
119         printf("empty\n");
120         return head;
121     }
122     if(head->num==m)
123     {
124         present=head;
125         head=head->next;
126         free(present);
125         head=head->next;
126         free(present);
127     }
128     else
129     {
130     present=head;
131     after=head->next;
132     while(after!=NULL&&after->num!=m)
133     {
134         present=after;
135         after=after->next;
136     }
137     if(after != NULL)
138     {
139         present->next = after->next;
140         free(after);
141     }
142     else
143         printf("no find");
144     }
145 
146     return head;
147 
148 }
149 
150 void print(stu *head)
151 {
152     stu *p;
153     p=head;
154     while(p!=NULL)
155     {
156         printf("%d %.2f\n",p->num,p->score);
157         p=p->next;
158     }
159 
160 }
161 
162 
                                                                                                          125,3-9       95%
                                                                                                         95,6-18       72%
                                                                                                                      96,3-12       49%                                                                                                                      33,1-4        24%
                                                                                                                  1,1          顶端
0 0
原创粉丝点击