带头节点的链表的一些操作

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<errno.h>
  4
  5 struct data
  6 {
  7     int c;
  8     int chinese;
  9 };
 10
 11 struct student
 12 {
 13     struct data DATA;
 14     struct student *next;
 15 };
 16 //下面函数声明
 17 struct student *create_link(struct student *head,int n);//创建链表函数
 18 void print_link(struct student *head);//打印链表
 19 struct student *insert_node(struct student *head,int n);//插入
 20 struct student *delete_node(struct student *head,int n);//删除
 21 void Search_node(struct student *head,int n);//查找
 22 struct student *sort1_node(struct student *head);//排序
 23 void write_file(struct student *head);//写
 24 struct student * read_file(void);//读
 25 struct student *select_sort(struct student *head);//选择排序
 26 int link_length(struct student *head);
 27 struct student *bubble1_sort(struct student *head);
 28
 29
 30 //主函数
 31 int main()
 32 {
 33     struct student *head=NULL;
 34     head=(struct student *)malloc(sizeof(struct student));//为头节点分配内存空间
 35     head=create_link(head,5);
 36     print_link(head);
 37     //插    
 38 /*  int a;
 39     printf("插入学生到位置:");
 40     scanf("%d",&a);
 41     head=insert_node(head,a);
 42     print_link(head);
 43     printf("\n");
 44     
 45     //删    
 46     int i;
 47     printf("需要删除学生的位置:");
 48     scanf("%d",&i);
 49     head=delete_node(head,i);
 50     print_link(head);
 51     printf("\n");
 52     
 53     //查
 54     int b;
 55     printf("查找学生到位置:");
 56     scanf("%d",&b);
 57     Search_node(head,b);

 58     printf("\n");
 59 */
 60
 61
 62
 63     //write_file(head); //写文件
 64
 65     //head=read_file(); //读文件
 66     //print_link(head);
 67
 68     //选择排序
 69 /*  select_sort(head);
 70     printf("排序后:\n");
 71     print_link(head);
 72 */
 73
 74     bubble1_sort(head);
 75     printf("排序后:\n");
 76     print_link(head);
 77
 78     return 0;
 79 }
 80
 81
 82 //创建链表
 83 struct student *create_link(struct student *head,int n)
 84 {
 85     if(NULL==head)//这里的NULL==head只是判断head有没有分配空间
 86     {
 87         head=(struct student *)malloc(sizeof(struct student));
 88         if(head==NULL)
 89         {
 90             printf("the memory is not big enough\n");
 91             return NULL;
 92         }
 93     }       // 上面的两个if有没有都行,只是容错处理!  
 94     struct student *p,*temp;
 95     p=head;
 96     int i;
 97     for(i=0;i<n;i++)
 98     {
 99         temp=(struct student *)malloc(sizeof(struct student));
100         printf("请输入第%d个节点的C语言成绩:",i);
101         scanf("%d",&temp->DATA.c);
102         printf("请输入第%d个节点的Chinese语言成绩:",i);
103         scanf("%d",&temp->DATA.chinese);
104         printf("\n");
105         p->next=temp;//将新申请的节点链接到链表上
106         p=temp;     //p的指正向下移动一格,就是将p的指针移到链表的尾部
107         p->next=NULL;   //将最后一个节点的next指针制空
108         temp=NULL;
109

110     }

111     return head;
112
113 }
114
115 //打印链表
116 void print_link(struct student *head)
117 {
118     if(head==NULL)
119     {
120         printf("the link is empty\n");
121         return  ;
122     }       //这上面的也是容错处理!
123     struct student *p;
124     p=head->next;//因为是带头节点的链表,所以有head节点,head里面是不存数据的,单纯的表示一个链表的头
125     while(p!=NULL)
126     {
127         printf("%d,%d   ",p->DATA.c,p->DATA.chinese);
128         p=p->next;
129     }
130     printf("\n");
131     return ;
132 }
133
134 //插入结点
135 struct student *insert_node(struct student *head,int n)
136 {
137     if( head == NULL && head->next == NULL )
138         perror("the link is empty\n");
139     int i=1;
140     struct student *p,*temp;
141     p=head;
142     temp=(struct student *)malloc(sizeof(struct student));
143
144     if( temp == NULL)   //容错处理
145         perror("the link is empty\n");
146
147     printf("请输入插入学生的C语言成绩:");
148     scanf("%d",&temp->DATA.c);
149     printf("请输入插入学生的Chinese语言成绩:");
150     scanf("%d",&temp->DATA.chinese);
151
152     while(p != NULL)
153     {
154         if(i==n && p->next!=NULL)
155         {
156             temp->next=p->next;
157             p->next=temp;
158             return head;
159         }
160         if(i==n && p->next==NULL)
161         {
162             p->next=temp;
163             temp->next=NULL;
164             return head;
165         }
166         i++;
 167         p=p->next;
168     }
169
170     if( p == NULL )//容错处理
171     {
172         printf("the link length is not big then n!\n");
173     }
174 }
175
176 //删除
177 struct student *delete_node(struct student *head,int n)
178 {
179     if(head==NULL)
180         perror("the link is empty\n");
181     struct student *temp,*p;
182     p = head;
183     temp=p->next;
184     int i=1;
185     while(p != NULL && temp != NULL)
186     {
187         if(i==n && temp->next!=NULL)
188         {
189             p->next=temp->next;
190             free(temp);
191             temp=NULL;
192             return head;
193         }
194         if(i==n && temp->next == NULL)
195         {
196             printf("hello\n");
197             p->next=NULL;
198             free(temp);
199             temp=NULL;
200             return head;
201         }
202         p=p->next;
203         temp=temp->next;
204         ++i;
205     }
206     return ;
207 }
208
209 //查找
210 void Search_node(struct student *head,int n)
211 {
212     if(NULL==head && NULL==head->next)
213         perror("the link is empty\n");
214     struct student *p;
215     p = head->next;
216     int i=1;
217     while(p != NULL )
218     {
219         if(i==n)
220         {
 221             printf("%d,%d\n",p->DATA.c,p->DATA.chinese);
222             return ;
223         }
224         i++;
225         p = p->next;
226     }
227     printf("the node is not exit!");
228     return ;
229 }
230
231
232
233 int link_length(struct student *head)
234 {
235     struct student *p=NULL;
236     int i=0;
237     p=head->next;
238     while(p!=NULL)
239     {
240         p=p->next;
241         i++;
242     }
243     return i;
244 }
245
246
247
248 //选择排序
249 struct student *select_sort(struct student *head)
250 {
251     if(NULL==head && NULL==head->next)
252         perror("the link is empty\n");
253     struct student *p=NULL,*q=NULL,*max=NULL;
254     struct data temp;
255     p=head->next;
256     while(p != NULL)
257     {
258         max=p;
259         q=p->next;
260         while(q!=NULL)
261         {
262             if(max->DATA.c < q->DATA.c)
263             {
264                 max=q;
265             }
266             q=q->next;
267         }
268         if(p!=max)
269         {
270             temp=p->DATA;
271             p->DATA=max->DATA;
272             max->DATA=temp;
273         }
274         p=p->next;
 275     }
276     return head;
277 }
278
279 //冒泡排序1  使用指针的
280 struct student *bubble1_sort(struct student *head)
281 {
282     struct student *p,*q;
283     int i,j;
284     int len = link_length(head);
285     for(i=1;i<len;i++)
286     {
287         p=head;
288         q=p->next;
289         for(j=0;j<len-i;++j)
290         {
291
292             if(q->DATA.c < q->next->DATA.c)
293             {
294                 p->next = q->next;
295                 q->next = p->next->next;
296                 p->next->next = q;
297             }
298             if((q->DATA.c < q->next->DATA.c) && (q->next->next = NULL))
299             {
300                 p->next = q->next;
301                 q->next = NULL;
302                 p->next->next = q;
303                 break;
304             }
305
306
307             p = p->next;
308             q = p->next;
309         }
310         p=NULL;
311         q=NULL;
312     }
313     return head;
314 }
315
316 //冒泡排序2
317 struct student *bubble2_sort(struct student *head)
318 {
319     struct student *p = NULL;
320     struct data temp;
321     int i,j;
322     int len=link_length(head);
323     for(int=1;i<len;++i)
324     {
325         p=head->next;
326         for(j=0;j<len-i;++j)
327         {
328             if(p->DATA.c > p->next->DATA.c)
 329             {
330                 temp=p->DATA;
331                 p->DATA=p->next->DATA;
332                 p->next->DATA=temp;
333             }
334             p=p->next;
335         }
336     }
337     return head;
338 }
339
340 //写文件
341 void write_file(struct student *head)
342 {
343     FILE *fp;
344     fp=fopen("student.txt","w");
345     if(fp==NULL)
346         perror("open failed\n");
347     struct student *p;
348     p=head->next;
349     int ret;
350     while(p!=NULL)
351     {
352         ret=fwrite(p,sizeof(struct student),1,fp);
353         if(ret<=0)
354             perror("write file failed\n");
355         p=p->next;
356     }
357     fclose(fp);
358 }
359
360 //读文件
361 struct student * read_file(void)
362 {
363     struct student *head=NULL;
364     FILE *fp;
365     fp=fopen("student.txt","r");
366     head=(struct student *)malloc(sizeof(struct student));
367     if(NULL == head)
368         perror("the memory is not big enough\n");
369     struct student *p,*temp;
370     p=head;
371     temp=(struct student *)malloc(sizeof(struct student));
372     while(fread(temp,sizeof(struct student),1,fp)>0)
373     {
374         p->next=temp;
375         p=p->next;
376         temp=NULL;
377         temp=(struct student *)malloc(sizeof(struct student));
378     }
379     p=NULL;
380     fclose(fp);
381     return head;
382 }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值