:: BO2TAY COLLECTIONS ::
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

danh sach lien ket day so nguyen

Go down

danh sach lien ket day so nguyen Empty danh sach lien ket day so nguyen

Bài gửi by thaihuuvt Sat Dec 08, 2007 6:53 pm

Code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>

typedef struct Node
{
    int info;
    Node * next;
};

//--------------------------------------

void Khoi_tao(Node *&h, Node *&t)
{
    h = t= NULL;
}

//--------------------------------------

void Them_dau(Node *&h, Node *&t, int x)
{
    Node *p = new Node;
    p -> info = x;
    p -> next = h;
    if (h == NULL) t = p;
    h = p;
}

//--------------------------------------

void Them_cuoi(Node *&h, Node *&t, int x)
{
    Node *p = new Node;
    p -> info = x;
    p -> next = NULL;
    if (!h) h = p;
    else t -> next = p;
    t = p;
}

//---------------------------------------

void Xuat(Node *h)
{
    for(Node *p = h; p; p = p -> next)
      if (p->next)
   printf("%d->", p->info);
      else printf("%d", p->info);
}

//---------------------------------------

void Them_sau_q(Node *q, Node *&t,int x)
{

    Node *p = new Node;

    p -> info = x;
    p -> next = q -> next;
    q -> next = p;
    if (q == t) t = p;

}

//---------------------------------------
int Xoa_dau(Node *&h)
{
   if(!h)
      return 0;

   Node *p=new Node;
   p->info=h->info;
   p->next=h->next;
   h=h->next;
   delete p;
   return 1;


}
//---------------------------------------

int Xoa_cuoi(Node *&t,Node *&h)
{
   if(!h)return 0;
   Node *lui=new Node;
   for(Node *p=h;p;p=p->next)
       if(p->next!=NULL)
         lui=p;
   lui->next=NULL;
   t=lui;
   delete p,lui;
   return 1;
}
//----------------------------------------
int Xoa_q(Node *&q,Node *&h)
{
   if(!h)return 0;
   Node *toi=new Node;
   for(Node *p=h;p;p=p->next)
       { toi=p->next;
         if(toi==q)
      p->next=toi->next;
       }
   delete p,toi;
   return 1;
}
//---------------------------------------
int dem_pt(Node *h)
{
   int dem=0;
   for(Node *p=h;p;p=p->next)
       dem++;
   return (dem);
}

//---------------------------------------
void main()
{
    Node *h, *t;
    clrscr();
    Khoi_tao(h, t);
    int chon,x,dung=0;int dem;
    do{

    printf("\n0:thoat \n1:Them dau \n2:Them cuoi \n3:Them giua \n4:Xoa dau \n5:Xoa cuoi \n6:Xoa giua \n7:Xem danh sach");
    scanf("%d",&chon);
    switch(chon)
      {
   case 0:
      dung=1;
      break;
   case 1:

      do{
      printf("\nnhap vao x: ");
      scanf("%d",&x);
      if(x)
         Them_dau(h,t,x);
        }while(x);
      break;
   case 2:
      do{
      printf("\nnhap vao x: ");
      scanf("%d",&x);
      if(x)
         Them_cuoi(h,t,x);
      }while(x);
      break;
   case 3:
      int q=0,x;
      //int q,x;printf("\nnhap q: ");scanf("%d",&q);
      printf("\nnahp x:");scanf("%d",&x);
      dem=dem_pt(h);
      for(Node*i=h;i;i=i->next)
         { q++;
           if(q==dem/2)
             //if(i->info==q)
             Them_sau_q(i,t,x);
         }
      break;
   case 4:
      int xoa=Xoa_dau(h);
      if(xoa)printf("\nxoa thanh cong");
      else printf("\ndanh sach rong ko xoa dc");
      break;
   case 5:
      int Xoa=Xoa_cuoi(t,h);
      if(Xoa==1)printf("Phan tu cuoi da duoc xoa.");
      else printf("danh sach rong,ko xoa duoc.");
      break;
   case 6:
      int d=0,dem=dem_pt(h),xg;
      for(Node *j=h;j;j=j->next)
          {
         d++;
         if(d==dem/2+1)
            xg=Xoa_q(j,h);
          }
      if(xg==1)printf("Xoa thanh cong");
      else ("Danh sach rong ko xoa duoc");
      break;
   case 7:
      printf("\n:Danh sach lien ket vua nhap: ");
      Xuat(h);
      break;
      }
      }while(dung!=1);
}
thaihuuvt
thaihuuvt
Chicken little !
Chicken little !

Tổng số bài gửi : 3
Registration date : 29/11/2007

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết