Danh sách liên kết đơn

Go down

Danh sách liên kết đơn

Bài gửi by 5XXXXXXX on Sat Dec 01, 2007 2:22 pm

Code:

#include
#include
#include

typedef struct Node
{
int info;
Node * next;
};

//--------------------------------------

void Khoi_tao(Node *&h, Node *&t)
{
h = t= NULL;
}

//--------------------------------------

void Them_dau(Node *&h, Node *&t, int x)
{
Node *p = new Node;
p -> info = x;
p -> next = h;
if (h == NULL) t = p;
h = p;
}

//--------------------------------------

void Them_cuoi(Node *&h, Node *&t, int x)
{
Node *p = new Node;
p -> info = x;
p -> next = NULL;
if (!h) h = p;
else t -> next = p;
t = p;
}

//---------------------------------------

void Xuat(Node *h)
{
for(Node *p = h; p; p = p -> next)
if (p->next)
printf("%d->", p->info);
else printf("%d", p->info);
}

//---------------------------------------

void Them_sau_q(Node *q, Node *&t,int x)
{

Node *p = new Node;

p -> info = x;
p -> next = q -> next;
q -> next = p;
if (q == t) t = p;

}

//---------------------------------------
int Xoa_dau(Node *&h)
{
if(!h)
return 0;

Node *p=new Node;
p->info=h->info;
p->next=h->next;
h=h->next;
delete p;
return 1;


}
//---------------------------------------

int Xoa_cuoi(Node *&t,Node *&h)
{
if(!h)return 0;
Node *lui=new Node;
for(Node *p=h;p;p=p->next)
if(p->next!=NULL)
lui=p;
lui->next=NULL;
t=lui;
delete p;
return 1;
}
//----------------------------------------
int Xoa_q(Node *&q,Node *&h)
{
if(!h)return 0;
Node *toi=new Node;
for(Node *p=h;p;p=p->next)
{ toi=p->next;
if(toi==q)
p->next=toi->next;
}
delete p;
return 1;
}
//---------------------------------------
int dem_pt(Node *h)
{
int dem=0;
for(Node *p=h;p;p=p->next)
dem++;
return (dem);
}

//---------------------------------------
void main()
{
Node *h, *t;
clrscr();
Khoi_tao(h, t);
int chon,x,dung=0;int dem;
do{

printf("\n0:thoat \n1:Them dau \n2:Them cuoi \n3:Them giua \n4:Xoa dau \n5:Xoa cuoi \n6:Xoa giua \n7:Xem danh sach");
scanf("%d",&chon);
switch(chon)
{
case 0:
dung=1;
break;
case 1:

do{
printf("\nnhap vao x: ");
scanf("%d",&x);
if(x)
Them_dau(h,t,x);
}while(x);
break;
case 2:
do{
printf("\nnhap vao x: ");
scanf("%d",&x);
if(x)
Them_cuoi(h,t,x);
}while(x);
break;
case 3:
int q=0,x;
printf("\nnahp x:");scanf("%d",&x);
dem=dem_pt(h);
for(Node*i=h;i;i=i->next)
{ q++;
if(q==dem/2)
Them_sau_q(i,t,x);
}
break;
case 4:
int xoa=Xoa_dau(h);
if(xoa)printf("\nxoa thanh cong");
else printf("\ndanh sach rong ko xoa dc");
break;
case 5:
int Xoa=Xoa_cuoi(t,h);
if(Xoa==1)printf("Phan tu cuoi da duoc xoa.");
else printf("danh sach rong,ko xoa duoc.");
break;
case 6:
int d=0,dem=dem_pt(h),xg;
for(Node *j=h;j;j=j->next)
{
d++;
if(d==dem/2+1)
xg=Xoa_q(j,h);
}
if(xg==1)printf("Xoa thanh cong");
else ("Danh sach rong ko xoa duoc");
break;
case 7:
printf("\n:Danh sach lien ket vua nhap: ");
Xuat(h);
break;
}
}while(dung!=1);


}
avatar
5XXXXXXX
Chicken little !
Chicken little !

Tổng số bài gửi : 7
Registration date : 20/11/2007

Xem lý lịch thành viên

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết