首页 > 其他 > 详细

2120=数据结构实验之链表五:单链表的拆分

时间:2019-02-28 13:46:55      阅读:166      评论:0      收藏:0      [点我收藏+]
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 struct node
 5 {
 6     int data;
 7     struct node*next;
 8 };
 9 int main()
10 {
11     int n,i,j=0,o=0;//o、j分别用来统计偶数和奇数的个数。
12     scanf("%d",&n);//题目比较简单,但是很繁琐,代码基本是重复的。
13     struct node*head,*end,*p,*head1,*head2,*end1,*end2;//这里没必要这么繁琐,可以自行简化。
14     head=(struct node*)malloc(sizeof(struct node));
15     head->next=NULL;
16     end=head;
17     for(i=0; i<n; i++)
18     {
19         p=(struct node*)malloc(sizeof(struct node));
20         scanf("%d",&p->data);
21         p->next=NULL;
22         end->next=p;
23         end=p;
24     }
25     head1=(struct node*)malloc(sizeof(struct node));
26     head1->next=NULL;
27     end1=head1;
28     head2=(struct node*)malloc(sizeof(struct node));
29     head2->next=NULL;
30     end2=head2;
31     for(p=head->next; p; p=p->next)
32     {
33         if(p->data%2==0)//对数据进行判断。
34         {
35             end1->next=p;
36             end1=p;
37             o++;
38         }
39         else
40         {
41             end2->next=p;
42             end2=p;
43             j++;
44         }
45     }
46     end1->next=NULL;end2->next=NULL;//保证链表有结尾,否则会输出奇奇怪怪的东西。
47     printf("%d %d\n",o,j);//这个地方别漏掉,同时还要注意输出的顺讯。
48     for(p=head1->next; p; p=p->next)
49     {
50 
51         printf("%d",p->data);
52         if(p->next!=NULL)printf(" ");
53     }
54     printf("\n");
55     for(p=head2->next; p; p=p->next)
56     {
57 
58         printf("%d",p->data);
59         if(p->next!=NULL)printf(" ");
60     }
61     return 0;
62 }

 

2120=数据结构实验之链表五:单链表的拆分

原文:https://www.cnblogs.com/Angfe/p/10449404.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!