#include <stdio.h>
#include <conio.h>
typedef struct node
{
        int data;
        node *next;
};
void xem(node *first)
{
     node *p;
    p=first;
    while(p!=NULL)
    {
                  printf("%d",p->data);
                  p=p->next;
    }
}
node *nhap(node *first,node *last,int n)
{
     node *p;
     for(int i=0;i<n;i++)
     {
             p=new(node);
             scanf("%d",&p->data);
             p->next=NULL;
             if(first==NULL)
             {
                            first=p;
                            last=p;
             }
             else
             {
                 last->next=p;
                 last=p;
             }
     }
     return first;
}

node *nhapx(node *first)
{
     node *x,*p;
     x=new(node);
     printf("nhap so x vao la : ");
     scanf("%d",&x->data);
     x->next=NULL;
     p=first;
     while(p->next!=NULL)
     p=p->next;
     p->next=x;
     return first;
}
node *sapxep(node *first)
{
     node *p1,*p2;int tg;
     p1=first;
     while(p1->next!=NULL)
     {
                          p2=p1->next;
                          while(p2!=NULL)
                          {
                                         if(p1->data>p2->data)
                                         {
                                                              tg=p1->data;
                                                              p1->data=p2->data;
                                                              p2->data=tg;
                                         }
                          p2=p2->next;
                          }
     p1=p1->next;
     }
     return first;
}
main()
{
      node *first=NULL,*last=NULL;
      int n,x;
      printf("nhap n: ");
      scanf("%d",&n);
      first=nhap(first,last,n);
      printf("\nxem: \n");
      xem(first);
      first=sapxep(first);
      printf("\nxem sau khi sap xep : ");
      xem(first);
      first=nhapx(first);
      first=sapxep(first);
      printf("\nxem sau khi them x vao,sap xep : ");
      xem(first);
      getch();
}
   

Bài viết liên quan

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

Đăng nhận xét