Tuesday , 21 March 2023
Home » Tự học Lập Trình » Cấu trúc dữ liệu và giải thuật » Danh sách liên kết FIFO (sắp xếp, tìm kiếm)

Danh sách liên kết FIFO (sắp xếp, tìm kiếm)

danh-sach-lien-ket-fifo

Danh sách liên kết FIFO (sắp xếp, tìm kiếm)

Bài 1. Tạo danh sách fifo gồm các số thực. sắp xếp chúng theo thứ tự không giảm. Nhập số thực x từ bàn phím, kiểm tra xem x có xuất hiện trong danh sách không

Download Code “Danh sách liên kết FIFO trong Visual C++”  (Project VC++ 2010 Express) 

#include <stdio.h>
#include <conio.h>
typedef struct node
{
float info;
node *link;
};
void xemfifo(node *f)
{
node *p;
p=f;
while (p!=NULL)
{
printf(“%5.2f”,p->info);
p=p->link;
}
}
node *nhapfifo(node *f,node *l,int n)
{
node *p;
for (int i=0;i<n;i++)
{
p=new(node);
scanf(“%f”,&p->info);
p->link=NULL;
if (f==NULL)
{
f=p;
l=p;
} else
{
l->link=p;
l=p;
}
}
return f;
}
void sapxep(node *f)
{
node *p1,*p2;
p1=f;
while (p1->link!=NULL)
{
p2=p1->link;
while (p2!=NULL)
{
if (p1->info>p2->info)
{
float tg=p1->info;
p1->info=p2->info;
p2->info=tg;
}
p2=p2->link;
}
p1=p1->link;
}
}
node *timkiem(node *f,float x)
{
node *p;
p=f;
while (p!=NULL && p->info!=x) p=p->link;
return p;
}
int main()
{
node *f=NULL,*l=NULL;
float x;
int n;
printf(“\nNhap so phan tu cua ds lien ket: “);
scanf(“%d”,&n);
f=nhapfifo(f,l,n);
printf(“\nDanh sach vua nhap \n”);
xemfifo(f);
printf(“\nSap xep danh sach khong giam “);
sapxep(f);
printf(“\nDanh sach sau khi sap xep \n”);
xemfifo(f);
printf(“\nNhap phan tu can kiem tra :”);
scanf(“%f”,&x);
if (timkiem(f,x)==NULL) printf(“\nkhong tim thay”);
else printf(“\nTim thay %5.2f trong danh sach fifo”,x);
getch();
}

Kết quả chương trình Danh sách liên kết FIFO trong Visual C++” 

sanh-sach-FIFO

Lượt xem (2669)

About Nhat MInh

Nhat MInh
Love Computer

Xem thêm

header in c++

Tách chương trình lớn thành các file riêng

Tại sao cần phải tách Khi lập trình với bất cứ ngôn ngữ nào thì …

Để lại bình luận:

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *