WA 求大神!!1

[复制链接]
查看11 | 回复0 | 2021-1-27 06:07:46 | 显示全部楼层 |阅读模式
数据结构实验之链表五:单链表的拆分
TimeLimit:1000MSMemoryLimit:65536KB
SubmitStatistic
ProblemDescription
输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据的相对次序与原链表一致。
Input
第一行输入整数N;;
第二行依次输入N个整数。
Output
第一行分别输出偶数链表与奇数链表的元素个数;
第二行依次输出偶数子链表的所有数据;
第三行依次输出奇数子链表的所有数据。
ExampleInput
10
132281599994461001
ExampleOutput
46
228446
131599991001


#include
#include
structnode
{
intdata;
structnode*next;
};
intmain()
{
intn,a=0,b=0;
structnode*head,*head1,*head2,*p,*q,*t1,*t2;
scanf("%d",&n);
head=(structnode*)malloc(sizeof(structnode));
head1=(structnode*)malloc(sizeof(structnode));
head2=(structnode*)malloc(sizeof(structnode));
head->next=NULL;
head2->next=NULL;
head1->next=NULL;
q=head;
while(n--)
{
p=(structnode*)malloc(sizeof(structnode));
scanf("%d",&p->data);
q->next=p;
q=p;
}
p=head->next;
q=p->next;
head->next=NULL;
t1=head1;t2=head2;
while(p!=NULL)
{
if(p->data%2==0)
{
t1->next=p;
t1=p;
a++;
}
else
{
t2->next=p;
t2=p;
b++;
}
p=q;
if(q!=NULL)
q=q->next;
}
p=head1->next;
q=head2->next;
printf("%d%d\n",a,b);
while(p!=NULL)
{
printf("%d",p->data);
if(p->next!=NULL)
printf("");
else
printf("\n");
p=p->next;
}
while(q!=NULL)
{
printf("%d",q->data);
if(q->next!=NULL)
printf("");
else
printf("\n");
q=q->next;
}
return0;
}


分 -->
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行