#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,int n)
{
     node *p;
     for(int i=0;i<n;i++)
     {
             p=new(node);
             scanf("%d",&p->data);
             p->next=first;
             first=p;
     }
     return first;
}
node *themdau(node *first)
{
     node *p;
     p=new(node);
     printf("\nnode can them vao dau: ");
     scanf("%d",&p->data);
     if(first==NULL)
     first=p;
     p->next=first;
     first=p;
     return first;
}

node *themcuoi(node *first)
{
     node *p,*tg;
     p=first;
     tg=new(node);
     printf("\nnode can them vao cuoi: ");
     scanf("%d",&tg->data);
     while(p->next!=NULL)
     p=p->next;
     tg->next=NULL;
     p->next=tg;
     return first;
}

node *bosungk(node *first)
{
     node *m,*tg;
     int dem=1,k;
     m=first;
     tg=new(node);
     printf("\nso can them vao k: ");
     scanf("%d",&tg->data);
     printf("\nnhap phan tu thu k muon bo sung la:");
     scanf("%d",&k);
     while(dem<k-1)
     {
                 m=m->next;
                 dem++;
     }
     tg->next=(m->next)->next;
     m->next=tg;
     return first;
}

node *bosungtruock(node *first)
{
     node *m,*tg,*p;
     int dem=1,k;
     m=first;
     tg=new(node);
     printf("\nso can them vao k: ");
     scanf("%d",&tg->data);
     printf("\nnhap phan tu thu k muon bo sung truoc k la:");
     scanf("%d",&k);
     while(dem<k-1)
     {
                 m=m->next;
                 dem++;
     }
     tg->next=(m->next);
     m->next=tg;
     return first;
}
node *bosungsauk(node *first)
{
     node *m,*tg,*p;
     int dem=1,k;
     m=first;
     tg=new(node);
     printf("\nso can them vao k: ");
     scanf("%d",&tg->data);
     printf("\nnhap phan tu thu k muon bo sung sau k la:");
     scanf("%d",&k);
     while(dem<k)
     {
                 m=m->next;
                 dem++;
     }
     tg->next=(m->next);
     m->next=tg;
     return first;
}
main()
{
      int n;
      node *first=NULL,*p=NULL;
      printf("\nnhap n la :\n");
      scanf("%d",&n);
      first=nhap(first,n);
      printf("\nxem: ");
      xem(first);
      first=themdau(first);
      printf("\nxem: ");
      xem(first);
      first=themcuoi(first);
      printf("\nxem: ");
      xem(first);
      first=bosungk(first);
      printf("\nxem: ");
      xem(first);
      first=bosungtruock(first);
      printf("\nxem: ");
      xem(first);
      first=bosungsauk(first);
      printf("\nxem: ");
      xem(first);
      getch();
}

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

Đăng nhận xét