#include<iostream>
using namespace std;
typedef int Element;
struct Node
{
Element Num;
Node *parent;
Node *next;
};
class Lnode
{
public:
Lnode();
void creat();
bool find(Element);//判断某个元素是否在链表中
void Insert(Element );//在末尾添加
void Insert(Element,Element);//在某个元素后面插入一个元素
void Destroy(Element);//删除某个节点
void Connection(Lnode &);//将两个表连接
void ToString();//输出链表
~Lnode();
private :
Node *head;
};
Lnode::~Lnode()
{
delete head;
head=NULL;
}
bool Lnode::find(Element x)
{
Node *p=head;
while(p!=NULL)
if(p->Num==x)
return true;
else
p=p->next;
return false;
}
void Lnode::Insert(Element x,Element y)
{
Node *p=new Node;
Node *q=new Node;
q=head;
p->Num=y;
while(q->Num!=x)
q=q->next;
p->next=q->next;
q->next->parent=p;
q->next=p;
p->parent=q;
}
void Lnode::Connection(Lnode &Other)
{
Node *p=new Node;
p=this->head;
while(p->next!=NULL)
p=p->next;
p->next=Other.head;
Other.head->parent=p;
}
Lnode::Lnode()
{
head=new Node;
head->parent=NULL;
head->next=NULL;
}
void Lnode::Insert(Element x)
{
Node *q=new Node;
q=head;
Node *p=new Node;
p->Num=x;
while(q->next)
q=q->next;
q->next=p;
p->parent=q;
p->next=NULL;
}
void Lnode::Destroy(Element x)
{
Node *q=new Node;
q=head;
while(q->Num!=x)
q=q->next;
q->next->parent=q->parent;
q->parent->next=q->next;
}
void Lnode::ToString()
{
Node *To=head;
while(To!=NULL)
{
cout<<To->Num<<" ";
if(To->next==NULL)
break;
else
To=To->next;
}
cout<<endl;
while(To!=NULL)
{
cout<<To->Num<<" ";
To=To->parent;
}
cout<<endl;
}
void Lnode::creat()
{
Node *p=NULL;
Node *q=new Node;
Element x;
while(cin>>x,x)
{
if(p==NULL)
{
p=new Node;
p=head;
p->Num=x;
}
else
{
q->Num=x;
p->next=q;
q->parent=p;
p=q;
q=new Node;
}
}
p->next=NULL;
}