Một số bài danh sách liên kết fifo - Cấu trúc dữ liêu-giải thuật.

Monday, November 24, 2014



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
/// code
#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();
}
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

#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();
}
Bài 3. Tạo danh sách fifo gồm các số nguyên. sắp xếp chúng theo thứ tự không giảm . Nhập số nguyên x từ bàn phím, kiểm tra xem x có xuất hiện trong danh sách không
#include <stdio.h>
#include <conio.h>
typedef struct node
{
      int info;
      node *link;
};
void xemfifo(node *f)
{
      node *p;
      p=f;
      while (p!=NULL)
      {
            printf("%5d",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("%d",&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,int x)
{
      node *p;
      p=f;
      while (p!=NULL && p->info!=x) p=p->link;
      return p;
}
int main()
{
      node *f=NULL,*l=NULL;
      int n,x;
      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("%d",&x);
      if (timkiem(f,x)==NULL) printf("\nkhong tim thay");
      else printf("\nTim thay  %d  trong danh sach fifo",x);
      getch();
}


Bài 4. Tạo danh sách fifo gồm các số nguyên. sắp xếp chúng theo thứ tự không tăng . Nhập số nguyên x từ bàn phím, kiểm tra xem x có xuất hiện trong danh sách không
#include <stdio.h>
#include <conio.h>
typedef struct node
{
      int info;
      node *link;
};
void xemfifo(node *f)
{
      node *p;
      p=f;
      while (p!=NULL)
      {
            printf("%5d",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("%d",&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,int x)
{
      node *p;
      p=f;
      while (p!=NULL && p->info!=x) p=p->link;
      return p;
}
int main()
{
      node *f=NULL,*l=NULL;
      int n,x;
      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("%d",&x);
      if (timkiem(f,x)==NULL) printf("\nkhong tim thay");
      else printf("\nTim thay  %d  trong danh sach fifo",x);
      getch();
}

Bài 5. 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 . Thêm số nguyên x vào đầu. xóa phần tử đầu danh sách.
#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);
      f=xoadau(f);
      printf("\n\ndanh sach sau khi xoa dau la: ");
      xem(f);
      getch();
}






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

All comments [ 0 ]


Your comments