Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据的相对次序与原链表一致。
Input
第一行输入整数N;;
第二行依次输入N个整数。
Output
第一行分别输出偶数链表与奇数链表的元素个数;
第二行依次输出偶数子链表的所有数据;
第三行依次输出奇数子链表的所有数据。
Sample Input
10
1 3 22 8 15 999 9 44 6 1001
Sample Output
4 6
22 8 44 6
1 3 15 999 9 1001
Hint
不得使用数组!
Source
本题其实就是,建立三个链表,一个存储原数据,两个存储拆分数据;
代码一:因为在链表的题目中代码长度较前面的代码长很多,所以建议采用函数,一个还得程序员,不仅要写对代码还要让代码清晰,清晰地代码可以反应出程序员的思路清晰,进而也说明这个程序员的水平较高
#include <stdlib.h>
#include <stdio.h>
struct node
{
int a;
struct node *next;
};
void display(struct node*head);//输出函数;
struct node*found(int n);//建立链表函数
void division(struct node*head,struct node*head1,struct node*head2,int *a1,int *a2);
//链表拆分函数;
int main()
{
int n,a1 = 0,a2 = 0;
struct node *head;
struct node *head1,*head2;
head1 = (struct node*)malloc(sizeof(struct node));
head1 -> next = NULL;
head2 = (struct node*)malloc(sizeof(struct node));
head2 -> next = NULL;
//为存放拆分链表的头结点分配空间;
scanf("%d",&n);
head = found(n);//建立链表;
division(head,head1,head2,&a1,&a2);//拆分链表;
printf("%d %d\n",a1,a2);
display(head1);
display(head2);//输出;
return 0;
}
void display(struct node *head)
//链表输出函数;
{
struct node *p = head -> next;
while(p)
{
printf("%d%c",p->a,p->next?' ' :'\n');
p = p -> next;
}
}
struct node*found(int n)
//链表建立函数;
{
struct node *head,*p,*q;
head = (struct node*)malloc(sizeof(struct node));
head -> next = NULL;
//建立头结点;
q = head;
int i;
for(i=0;i<n;i++)
{
p = (struct node*)malloc(sizeof(struct node));
p -> next = NULL;
scanf("%d",&p -> a);
//建立空节点;
q -> next = p;
q = p;//链接;
}
return (head);
};
void division(struct node*head,struct node*head1,struct node*head2,int *a1,int *a2)
//链表拆分函数;
{
struct node *p = head -> next;
struct node *q1 = head1,*q2 = head2;
free(head);
//因为这个头节点已经不用,所以要释放内存,虽然不释放内存这个题目也对,
//但是作为程序员要养成良好的习惯;
while(p)
{
if(p -> a%2 == 0)//判断并连接
{
q1 -> next = p;
q1 = p;
p = p -> next;
q1 -> next = NULL;
(*a1)++;
}
else
{
q2 -> next = p;
q2 = p;
p = p -> next;
q2 -> next = NULL;
(*a2)++;
}
}
}
代码二:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int a;
struct node *next;
};
int main()
{
int a,b=0,c=0,x;
int i;
scanf("%d",&a);
struct node *head1,*head2,*head3,*q,*p;
head1 = (struct node *)malloc(sizeof(struct node));
head1 -> next =NULL;
q = head1;
for(i=0; i<a; i++) //建立存储原数据链表;
{
scanf("%d",&x);
p = (struct node *)malloc(sizeof(struct node));
p -> next =NULL;
p -> a = x;
q -> next = p;
q = p;
}
head2 = (struct node *)malloc(sizeof(struct node));
head2 -> next = NULL;
head3 = (struct node *)malloc(sizeof(struct node));
head3 -> next =NULL; //同时建立两个链表;
q = head1 -> next;
struct node *p1 = head2,*p2 = head3;
while(q)
{
p = (struct node *)malloc(sizeof(struct node));
p -> next = NULL;
p -> a = q -> a; //每次建立一个节点;
if((q -> a)%2==0) //如果是偶数连接在第二个链表上;
{
b++;
p1 -> next = p;
p1 = p;
}
else //否则连接在第三个链表上;
{
c++;
p2 -> next = p;
p2 = p;
}
q = q -> next;
}
p1 = head2 -> next;
p2 = head3 -> next;
printf("%d %d\n",b,c);
while(p1) //输出;
{
if(p1 -> next)
printf("%d ",p1 -> a);
else
printf("%d\n",p1 -> a);
p1 = p1 -> next;
}
while(p2)
{
if(p2 -> next)
printf("%d ",p2 -> a);
else
printf("%d\n",p2 -> a);
p2 = p2 -> next;
}
return 0;
}