* File main.cpp
#include"iostream"
using namespace std;
typedef struct tnode
{
int key;
struct tnode*pnext;
}node;
typedef struct tlist
{
node *head;node *tail;
}list;
node *getnode(int x)
{
node*t;
t=new node;
if(t==NULL)
{
cout<<"khong cap phat duoc";
exit(0);
}
t->key=x;
t->pnext=NULL;
return t;
}
void addhead(list &l,node*p)
{
if(l.head==NULL)
l.head=l.tail=p;
else
{
p->pnext=l.head;
l.head=p;
}
}
void addtail(list &l,node*p)
{
if(l.head==NULL)
l.head=l.tail=p;
else
{
l.tail->pnext=p;
l.tail=p;
}
}
void nhapds(list &l)
{
node *p;
int x,n;
l.head=l.tail=NULL;//khoi dong ds rong
cout<<"nhap vao so luong node: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"nhap gia tri x: ";
cin>>x;
p=getnode(x);
addtail(l,p); //addhead(l,p);
}
}
void xuatds(list l)
{
node*i;
for(i=l.head;i!=NULL;i=i->pnext)
cout<<i->key<<"->";
cout<<"NULL";
}
void huyds(list &l)
{
node *p=l.head;
while(p!=NULL)
{
l.head=p->pnext;
delete p;
p=l.head;
}
}
void main()
{
list l;
nhapds(l);
xuatds(l);
huyds(l);
}
Hay lắm, tks
ReplyDelete