![]() |
#2
小笨蛋一个2011-10-07 16:50
|
只有本站会员才能查看附件,请 登录
#include <iostream.h>
class jd
{
public:
jd(int n);
int data;
jd *next;
};
jd::jd(int n)
{
data=n;
next=NULL;
}
class list
{
public:
list();
list(int n);
insert(int n);
jd *head;
int length;
disp();
};
list::list()
{
head=NULL;
length=0;
}
list::list(int n)
{
head=new jd(n);
length=1;
}
list::insert(int n)
{
jd *p=new jd(n);
p->next=head;
head=p;
length++;
}
list::disp()
{
jd *p=head;
for(int i=0;i<=length;i++)
{
if(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}}
cout<<endl;
}
void main()
{
list l1(0);
list l2(0);
list l3(0);
int a;
cout<<"输入链表数据,以回车结束"<<endl;
while(!(a==0))
{
cin>>a;
l1.insert(a);
}
jd *p;
p=l1.head;
while(p)
{
if(p->data%2)
{
l2.insert(p->data);
}
else
{
l3.insert(p->data);
}
p=p->next;
}
l1.disp();
l2.disp();
l3.disp();
}