Toggle navigation
Home
Latest pastes
FAQ
Random
Register
Login
Untitled
SUBMITTED BY:
becausemon01
DATE:
June 26, 2017, 2:09 p.m.
FORMAT:
Text only
SIZE:
1.3 kB
Raw
Download
Tweet
HITS:
707
Go to comments
Report
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* pNext;
};
struct list
{
Node* pHead;
Node* pTail;
};
void khoaitao(list& l)
{
l.pHead=NULL;
l.pTail=NULL;
}
Node* getNode(int x)
{
Node* p= new Node;
if(p==NULL)
return NULL;
else {
p->data=x;
p->pNext=NULL;
}
}
void addHead(list &l, Node* p)
{
if(l.pHead==NULL)
l.pHead=l.pTail=p;
else {
p->pNext=l.pHead;
l.pHead=p;
}
}
void addTail(list &l, Node* p)
{
if(l.pHead==NULL)
l.pHead=l.pTail=p;
else {
l.pTail->pNext=p;
l.pTail=p;
}
}
void addAfter(list &l, int x, int y)
{
Node* p;
for(p=l.pHead;p!=NULL;p->pNext)
{
if(p->data==x)
{
Node* t=getNode(y);
p->pNext=t->pNext;
p->pNext=t;
t->data=y;
break;
}
else addHead(l,p);
}
}
void xuat(list l)
{
for(Node* p=l.pHead;p!=NULL;p=p->pNext)
{
cout<<p->data;
}
}
int main()
{
list l;
khoaitao(l);
int x;
do{
cin>>x;
if(x==3)
break;
else if(x==0)
{
int a;
cin>>a;
Node* p;
addHead(l, p =getNode(a));
}
else if(x==1)
{
int b;
cin>>b;
Node* p;
addTail(l,p=getNode(b));
}
else if(x==2)
{
int c,d;
cin>>c;
cin>>d;
addAfter(l,c,d);
}
}while(x!=3);
xuat(l);
return 0;
}
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus