Tạo danh sách fifo gồm các số nguyên. Sắp xếp chúng theo thứ tự tăng dần. Đồng thời thêm số nguyên x vào đầu danh sách; Xóa phần tử đầu trong danh sách và in ra màn hình.
Download code Danh sách liên kết FIFO, bổ sung, xóa phần tử
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
struct node *link;
};
node *p;
node *nhap(node *f,node *l,int n)
{
for(int i=0;i<n;i++)
{
p=new(node);
printf(“nhap so nguyen n= “);
scanf(“%d”,&p->info);
p->link=NULL;
if(f==NULL)
{
f=p;l=p;
}
else { l->link=p;l=p;}
} return f;
}
void xem(node *f)
{
p=f;
while(p!=NULL)
{
printf(“%5d”,p->info);
p=p->link;
}
}
node *sapxeptang(node *f)
{
node *p1,*p2;int tg;
p1=f;
while(p1->link!=NULL)
{
p2=p1->link;
while(p2!=NULL)
{
if(p1->info>p2->info)
{
tg=p1->info;
p1->info=p2->info;
p2->info=tg;
} p2=p2->link;
}p1=p1->link;
}return f;
}
node *bosungdau(node *f)
{
node *p;
p=new(node);
printf(“\n\nnhap phan tu bo sung dau: “);
scanf(“%d”,&p->info);
p->link=f;
f=p;
return f;
}
node *xoadau(node *f)
{
p=new(node);
p=f;
if(f==NULL) return f;
f=p->link;
delete (p);
return f;
}
int main()
{
node *f=NULL,*l=NULL;int n;
printf(“\nnhap n= “);
scanf(“%d”,&n);
f=nhap(f,l,n);
printf(“\n\ndanh sach vua nhap la: “);
xem(f);
f=sapxeptang(f);
printf(“\n\ndanh sach sau khi sap xep tang la: “);
xem(f);
f=bosungdau(f);
printf(“\n\ndanh sach sau khi bo sung dau la: “);
xem(f);
printf(“\n\ndanh sach sau khi xoa dau la: “);
f=xoadau(f);
xem(f);
getch();
}
Kết quả chương trình:Danh sách liên kết FIFO, bổ sung, xóa phần tử
Lượt xem (2271)
Để lại bình luận: