Làm lại bài khác do quên đăng hướng dẫn sử dụng :v
Trong bài này đã bổ xung thêm một số chương trình con xóa node
1.Chạy CT bằng dev, c-free...
2.Nhập số tới lúc mô ưng thì thôi tốt nhất nhập khoảng vài triệu đến vài tỉ phần tử =]]
3.Muốn ngưng nhập bấm 0 rồi enter
4.Chọn thao tác cần làm
5.Bước còn lại tự sử
Tags: [You must be registered and logged in to see this link.]
Trong bài này đã bổ xung thêm một số chương trình con xóa node
1.Chạy CT bằng dev, c-free...
2.Nhập số tới lúc mô ưng thì thôi tốt nhất nhập khoảng vài triệu đến vài tỉ phần tử =]]
3.Muốn ngưng nhập bấm 0 rồi enter
4.Chọn thao tác cần làm
5.Bước còn lại tự sử
- code:
- Code:
#include <conio.h>
#include <stdio.h>
typedef int item;
typedef struct node
{
item Data;
node *next;
};
typedef node *list;
node *tao_node(node *p,item x)
{
p=new node;
p->next=NULL;
p->Data=x;
return p;
}
int len(list l)
{
node *p=l;
int i=0;
while(p!=NULL)
{
i++;
p=p->next;
};
return i;
}
void chen_dau(list &l,item x)
{
node *p;
p=tao_node(p,x);
p->next = l;
l = p;
}
void chen_k(list &l,item x, int k)
{
node *p,*q=l;
if(k<1||k>(len(l)+1))printf("Vi tri chen khong hop le!!");
else
{
p = tao_node(p,x);
int i=1;
if(k==1)chen_dau(l,x);
else
{
while(q!=NULL&&i!=k-1)
{
q=q->next;
i++;
}
p->next=q->next;
q->next=p;
}
}
}
void nhap(list &l)
{
int i=1;
item x;
while(x!=0)
{
printf("Nhap phan tu thu %d : ",i);scanf("%d",&x);i++;
if(x!=0)chen_k(l,x,len(l)+1);
}
}
void init(list &l)
{
l=NULL;
}
void xuat(list l)
{
node *P=l;
if(P!=NULL)printf("\nDanh sach da nhap : \n");
while(P!=NULL)
{
printf(" %d ",P->Data);
P=P->next;
}
printf("\n");
}
void kiemrong(list l)
{
if(l==NULL)printf("Danh sach rong.\n");
else printf("Danh sach 0! rong.\n");
}
void chen_cuoi(list &l,item x)
{
chen_k(l,x,len(l)+1);
}
void xoa_dau(list &l)
{
l=l->next;
}
void xoa_k(list &l,int k)
{
node *p,*q=l;
int i=1;
if(k<1||k>(len(l)+1))printf("Xoa loi!!");
else
{
if(k==1)xoa_dau(l);
else
{
while(q!=NULL && i!=(k-1))
{
q=q->next;
i++;
}
q->next=q->next->next;
}
}
}
/*void xoa_x(list &l,item x)
{
node *a=l;
while (a->Data != x && a!=NULL)
{
a=a->next;
}
a->next=a->next->next;
printf("Da xoa!!");
}*/
int timx(list l,item x)
{
node *a=l;
int i=1;
while(a!=NULL && a->Data!= x)
{
a=a->next;
i++;
}
if(a!=NULL)return i;
else printf("Nhap loi~ roi ma'");
}
void xoa_xtim(list &l,item x)
{
int a;
a=timx(l,x);
xoa_k(l,a);
}
main()
{
list l;
init(l);
nhap(l);
xuat (l);
//
printf("----------------------Program-----------------------\n");
printf("\n1.Kiem tra danh sach co bao nhieu phan tu .\n");
printf("\n2.Kiem tra danh sach rong .\n");
printf("\n3.Chen phan tu vao dau danh sach .\n");
printf("\n4.Chen phan tu vao cuoi danh sach .\n");
printf("\n5.Xoa phan tu dau danh sach .\n");
printf("\n6.Xoa phan tu o vi tri k trong danh sach .\n");
printf("\n7.Xoa phan tu x .\n");
printf("\n8.tim x .\n");
printf("\n9.Xoa phan tu theo x.\n");
int chon;
printf("Ban chon : ");scanf("%d",&chon);printf("\n");
switch(chon)
{
case 1:
{
printf("Danh sach co %d phan tu .",len(l));break;
}
case 2:
{
kiemrong(l);break;
}
case 3:
{
item x;
printf("Nhap phan tu can chen :");
scanf("%d",&x);
chen_dau(l,x);
xuat(l);break;
}
case 4:
{
item x;
printf("Nhap phan tu can chen :");
scanf("%d",&x);
chen_cuoi(l,x);
xuat(l);break;
}
case 5:
{
xoa_dau(l);
xuat(l);break;
}
case 6:
{
int x;
printf("Nhap vi tri can xoa :");scanf("%d",&x);
xoa_k(l,x);
xuat(l);break;
}
case 7:
{
item x;
printf("Nhap pt can xoa :");scanf("%d",&x);
//xoa_x;
xuat(l);break;
}
case 8:
{
item x;
printf("Nhap PT can tim :");scanf("%d",&x);
printf("OVT : %d\n",timx(l,x));break;
}
case 9:
{
item x;
printf("Nhap PT can tim xoa :");scanf("%d",&x);
xoa_xtim(l,x);
xuat(l);break;
}
}
return 0;
}
Tags: [You must be registered and logged in to see this link.]