Advertisement
Guest User

Untitled

a guest
Dec 13th, 2010
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct pointer
  4.          { int info;
  5.            struct pointer *lptr;
  6.            struct pointer *rptr;
  7.          } dslr;
  8. dslr *l,*r,*p, *M;
  9.  
  10. int main()
  11. {
  12.     int a, i, x;
  13.     printf("\n Nhap so phan tu cua day: ");
  14.     scanf("%d",&a);
  15.     if(a==0) printf("\n Day rong");
  16.     else
  17. {
  18.     //tao ds
  19.     dslr *p, *l, *r, *m, *n;
  20.     m=NULL;
  21.     n=NULL;
  22.     for(i=1;i<=a;i++) //phat trien sang ben phai
  23.     {
  24.         p=(dslr*)malloc(sizeof(dslr));
  25.         if(m==NULL)
  26.         {
  27.             p->lptr=NULL;
  28.             m=p;
  29.         }
  30.         else
  31.         {
  32.             p->lptr=m;
  33.             m=p;
  34.         }
  35.     }
  36.     r=p;// dua r ve cuoi ds
  37.    
  38.     for(i=1;i<=a;i++) //phat trien sang ben trai
  39.     {
  40.         if(n==NULL)
  41.         {
  42.             p->rptr=NULL;
  43.             n=p;
  44.         }
  45.         else
  46.         {
  47.             p->rptr=n;
  48.             n=p;
  49.         }
  50.        
  51.         if(i<a)
  52.         {
  53.             p=p->lptr;
  54.         }
  55.     }
  56.     l=p;     // dua l len dau ds
  57.  
  58.     for(i=1;i<=a;i++) //nhap info cho ds, sau buoc nay p tro ve cuoi ds
  59.     {
  60.         printf("\n Nhap phan tu thu %d: ",i);
  61.         scanf("%d", &p->info);
  62.         p=p->rptr;
  63.     }
  64.     p=l; // dua p tro ve dau de in ra kq,  sau buoc nay p tro ve cuoi ds
  65.     printf("\n Day vua nhap la: ");
  66.     for(i=1;i<=a;i++)
  67.     {
  68.         printf("  %d",p->info);
  69.         p=p->rptr;
  70.     }
  71.        
  72.        
  73.     //them vao dau d.s
  74.     printf("\n Gia tri phan tu muon bo sung vao dau d.s: ");
  75.     scanf("%d",&x);
  76.    
  77.     M=l; //m o dau ds
  78.     p=(dslr*)malloc(sizeof(dslr));
  79.     p->info=x;
  80.         p->lptr=NULL;
  81.         p->rptr=M;
  82.         M->lptr=p;
  83.         l=p;
  84.            
  85.     printf("\n Day sau khi bo sung vao dau la: ");
  86.     for(i=1;i<=a+1;i++)
  87.     {
  88.         printf("  %d",p->info);
  89.         p=p->rptr;
  90.     }
  91.    
  92.    
  93.     //them cuoi
  94.     printf("\n Gia tri phan tu muon bo sung vao cuoi d.s: ");
  95.     scanf("%d",&x);
  96.     M=r;
  97.     p=(dslr*)malloc(sizeof(dslr));
  98.     p->info=x;
  99.         p->rptr=NULL;
  100.         p->lptr=M;
  101.         M->rptr=p;
  102.         r=p;
  103.         p=l;
  104.         printf("\n Day sau khi bo sung vao cuoi la: ");
  105.     for(i=1;i<=a+2;i++)
  106.     {
  107.         printf("  %d",p->info);
  108.         p=p->rptr;
  109.     }
  110.    
  111.     //xoa nut cuoi
  112.     M=r;
  113.     r=r->lptr;
  114.     r->rptr=NULL;
  115.     M->lptr=NULL;
  116.     printf("\n Day sau khi xoa nut cuoi: ");
  117.     p=l;
  118.     for(i=1;i<=a+1;i++)
  119.     {
  120.         printf("  %d",p->info);
  121.         p=p->rptr;
  122.     }
  123.    
  124.     //xoa nut dau
  125.    
  126.     M=l;
  127.     l=l->rptr;
  128.     l->lptr=NULL;
  129.     M->rptr=NULL;
  130.     printf("\n Day sau khi xoa nut dau: ");
  131.     p=l;
  132.     for(i=1;i<=a;i++)
  133.     {
  134.         printf("  %d",p->info);
  135.         p=p->rptr;
  136.     }
  137.    
  138.     //tim phan tu chan dau tien
  139.     p=l;
  140.     int t;
  141.     while(p!=NULL)
  142.     {
  143.         if(p->info%2==0)
  144.         {
  145.             t=p->info;
  146.             break;
  147.         }
  148.         p=p->rptr;
  149.     }
  150.  
  151.     if(p!=NULL)printf("\n Phan tu chan dau tien la : %d",p->info);
  152.     else printf("\n Khong co phan tu chan nao");
  153.    
  154. }
  155. return 1;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement