#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 *xoadau(node *first)
{
     node *p;
     p=first;
     while(first==NULL)
     return first;
     first=p->next;
     delete(p);
     return first;
}
node *xoacuoi(node *first)
{
     node *p,*q;
     p=first;
     while(p->next!=NULL)
     {
                   q=p;
                   p=p->next;
     }
   
     q->next=NULL;
     return first;
}

node *themdau(node *first)
{
     node *tg;
     tg=new(node);
     printf("nhap phan tu can them vao dau : ");
     scanf("%d",&tg->data);
     if(first==NULL)
     first=tg;
     tg->next=first;
     first=tg;
     return first;
}
node *themcuoi(node *first)
{
     node *p,*tg;
     tg=new(node);
     printf("nhap phan tu can them vao cuoi : ");
     scanf("%d",&tg->data);
     p=first;
     tg->next=NULL;
     while(p->next!=NULL)
     p=p->next;
     p->next=tg;
     return first;
}
main()
{
      int n;
      node *first=NULL,*last=NULL;
      printf("\nnhap n: \n");
      scanf("%d",&n);
      first=nhap(first,last,n);
      printf("\nxem: ");
      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