Guest User

Untitled

a guest
Jan 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream.h>
  2. #include<conio.h>
  3. //Alocare dinamica|sterge + adaugare element la o pozitie
  4. #define n 5 //n noduri
  5.  
  6.  
  7. struct nod{
  8.     int x;
  9.     nod *next;
  10. };
  11.  
  12. nod *p,*u;
  13.  
  14.  
  15.  
  16. void ins(int val){
  17.     nod *c,*a;
  18.     c=p;
  19.  
  20. if(p->x==val)
  21. {c=new nod;
  22.    cout<<"INS";
  23.    cin>>c->x;
  24.    c->next=p;
  25.    p=c;
  26. }else{
  27.     while(c->next->x!=val &&c)
  28.     c=c->next;
  29.     a=new nod;
  30.     cout<<"INS";
  31.     cin>>a->x;
  32.     a->next=c->next;
  33.     c->next=a;
  34. }
  35.  
  36. }
  37.  
  38.  
  39. void stergere(int val){
  40. nod *c,*a;
  41.  c=p;
  42.  
  43. if(p->x==val){
  44.  
  45.     a=p;        
  46.     p=p->next;
  47.     delete a;
  48.  
  49. }else{
  50.     while(c->next->x!=val &&c)
  51.         c=c->next;
  52.  
  53.      a=c->next;
  54.      c->next=a->next;
  55.      if(a==u) u=c;
  56.      delete a;
  57.  }
  58.  
  59. }
  60.  
  61.  
  62. void add(){
  63.     nod *c;
  64.     if(!p){
  65.         p=new nod;
  66.         cout<<"X:";
  67.         cin>>p->x;
  68.         u=p;          
  69.    
  70.     }else{
  71.      c=new nod;
  72.      cout<<"X:";
  73.      cin>>c->x;        
  74.      u->next=c;
  75.      u=c;
  76.     }
  77.  
  78.     u->next=0;
  79.  
  80. }
  81. void afis(){
  82.     nod *c;
  83.     c=p;
  84.     while(c){
  85.         cout<<c->x<<endl;
  86.         c=c->next;
  87.     }
  88. }
  89.  
  90. void main(){
  91.  
  92. for(int i=1;i<=n;i++)
  93.     add( );
  94.  
  95. ins(30);
  96. afis();
  97.  
  98.  
  99.  
  100. stergere(70);
  101. afis();
  102.  
  103.  
  104.  
  105. int x;
  106. cin>>x;
  107.  
  108. }
Add Comment
Please, Sign In to add comment