#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
node *head;
head = new node();
head->next = NULL;
int n;
cin>>n;
node *p,*q;
q = head;
for(int i=0;i<n;i++)
{
p = new node();
cin>>p->data;
p->next = q->next;
q->next = p;
q = p;
}
node *head1,*head2;
head1 = new node();
head1->next = NULL;
head2 =new node();
head2->next = NULL;
p = head->next;
int x = 0,y = 0;
node *w1 = head1,*w2 = head2;
while(p!=NULL)
{
int data = p->data;
node *now = new node();
now -> next = NULL;
now -> data = data;
if(data % 2 == 0)
{
w1->next = now;
w1 = now;
x++;
}
else
{
w2->next = now;
w2 = now;
y++;
}
p = p->next;
}
p = head1->next;
q = head2->next;
cout<<x<<" "<<y<<endl;
while(p)
{
if(p->next !=NULL)
cout<<p->data<<" ";
else
cout<<p->data<<endl;
p = p->next;
}
while(q)
{
if(q->next != NULL)
cout<<q->data<<" ";
else
cout<<q->data<<endl;
q = q->next;
}
return 0;
}
G - 数据结构实验之链表五:单链表的拆分
最新推荐文章于 2022-01-30 14:04:41 发布