Bài 2. Tạo danh sách fifo gồm các số thực. sắp xếp chúng theo thứ tự không tăng . 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

Thursday, November 27, 2014
Code c/c++

#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 tang ");
      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();

}
Chia sẻ bài viết ^^
Other post

All comments [ 0 ]


Your comments