#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 *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;
     delete(p);
     return first;
}
node *xoanodek(node *first)
{
     int k;
     node *m,*p;
     int dem=1;
     m=first;
   
     printf("\nnhap phan tu thu k muon xoa la :");
     scanf("%d",&k);
     while(dem<k)
     {
                 m=m->next;
                 dem++;
     }
     p=first;
     while(p!=NULL && p->next!=m)
     p=p->next;
     p->next=m->next;
     return first;
}
node *xoanodesauk(node *first)
{
     int k;
     node *m,*p;
     int dem=1;
     m=first;
   
     printf("\nnhap phan tu thu k ,muon xoa sau k la :");
     scanf("%d",&k);
     while(dem<=k)
     {
                 m=m->next;
                 dem++;
     }
     p=first;
     while(p!=NULL && p->next!=m)
     p=p->next;
     p->next=m->next;
     return first;
}
node *xoanodetruock(node *first)
{
     int k;
     node *m,*p;
     int dem=1;
     m=first;
   
     printf("\nnhap phan tu thu k ,muon xoa truoc k la :");
     scanf("%d",&k);
     while(dem<k-1)
     {
                 m=m->next;
                 dem++;
     }
     p=first;
     while(p!=NULL && p->next!=m)
     p=p->next;
     p->next=m->next;
     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=xoadau(first);
      printf("\nxem:");
      xem(first);
      first=xoacuoi(first);
      printf("\nxem: ");
      xem(first);
      first=xoanodek(first);
      printf("\nxem: ");
      xem(first);
      first=xoanodesauk(first);
      printf("\nxem: ");
      xem(first);
      first=xoanodetruock(first);
      printf("\nxem: ");
      xem(first);
      getch();
}

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

Đăng nhận xét