#include <stdio.h>
#include <conio.h>
typedef struct node
{
        int tu,mau;
        node *next;
};
void xem(node *first)
{
     node *p;
     p=first;
     while(p!=NULL)
     {
                   printf("%5d/%d",p->tu,p->mau);
                   p=p->next;
     }
}
void doicho(int *a,int *b)
{
     int tg;
     tg=*a;
     *a=*b;
     *b=tg;
}
node *nhap(node *first,node *last,int n)
{
     node *p;
     nhap:for(int i=0;i<n;i++)
     {
             p=new(node);
             scanf("%d%d",&p->tu,&p->mau);
             if(p->mau==0)
             {
                             printf("\nso khog hop le" );
                             first=NULL;
                             goto nhap;
             }
             p->next=NULL;
             if(first==NULL)
             {
                            first=p;
                            last=p;
             }
             else
             {
                 last->next=p;
                 last=p;
             }
     }
     return first;
}
void sapxep(node *first)
{
     node *p1,*p2;
     p1=first;
     while(p1->next!=NULL)
     {
                          p2=p1->next;
                          while(p2!=NULL)
                          {
                                         if(((float)p1->tu/p1->mau)>((float)p2->tu/p2->mau))
                                         {
                                             doicho(&p1->tu,&p2->tu);
                                             doicho(&p1->mau,&p2->mau);
                                         }
                          p2=p2->next;
                          }
     p1=p1->next;
     }
}

node *themdau(node *first)
{
     node *p;
     p=new(node);
     printf("nhap sao can them la : ");
     scanf("%d%d",&p->tu,&p->mau);
     p->next=first;
     first=p;
     return first;
}

node *themcuoi(node *first)
{
     node *p,*tg;
     tg=new(node);
     printf("nhap sao can them la : ");
     scanf("%d%d",&tg->tu,&tg->mau);
     tg->next=NULL;
     p=first;
     while(p->next!=NULL)
     p=p->next;
     if(first==NULL)
     first=tg;
     p->next=tg;
     return first;
}

node *xoadau(node *first)
{
     node *p;
     p=first;
     if(first==NULL)
     return first;
     first=p->next;
     delete(p);
     return first;
}

node *xoacuoi(node *first)
{
     node *p,*tg;
     p=first;
     while(p->next!=NULL)
     {
                         tg=p;
                         p=p->next;
     }
     tg->next=NULL;
     return first;
}
                       
main()
{
      node *first=NULL,*last=NULL;
      int n;
      printf("nhap n: ");
      scanf("%d",&n);
      first=nhap(first,last,n);
      printf("\nxem: \n");
      xem(first);
      sapxep(first);
      printf("\nxem sau khi sap xep: \n");
      xem(first);
      first=themdau(first);
      printf("\nxem khi them dau: ");
      xem(first);
      first=themcuoi(first);
      printf("\nxem khi them cuoi: ");
      xem(first);
      first=xoadau(first);
      printf("\nxem khi xoa dau: ");
      xem(first);
      first=xoacuoi(first);
      printf("\nxem khi xoa cuoi: ");
      xem(first);
      getch();
}

Bài viết liên quan

Không có nhận xét nào:

Đăng nhận xét