Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7.     int data;
  8.     node* link;
  9. };
  10.  
  11. void check(node* h);
  12. int menu(node* h);
  13. void pozicija(node* h);
  14. void newlist(node* h);
  15. void change (node* h);
  16. int trint (node*& h);
  17. void cls()
  18. {
  19. system ("CLS");
  20. }
  21. int main()
  22. {
  23. node* h=NULL;
  24. menu(h);
  25.     return 0;
  26. }
  27. void check(node* h)
  28. {
  29.     int i=1;
  30.     node* temp;
  31.     temp=h;
  32.     if (h!=NULL) {
  33. do{
  34. cout<<"Saraso skaicius "<<i<<": "<<temp->data<<endl;
  35.     temp=temp->link;
  36.     i++;
  37. }while(temp->link!=NULL);
  38. cout<<"Saraso skaicius "<<i<<": "<<temp->data<<endl<<endl<<endl;
  39. }
  40. else
  41. {   cls();
  42.     cout<<"Sarasas neegzistuoja\n\n";
  43. }
  44. }
  45. int menu(node* h)
  46. {
  47. int i=0;
  48.     cout<<"pasirinkite funkcija: \n\n0) Isjungti programa\n1) atspausdinti sarasa\n2) iterpti nauja elementa i nurodyta pozicija\n3) iterpti elementa vietoj tam tikro elemento\n4) Sukurti nauja sarasa\n5) Istrinti dabartini sarasa\n"<<endl;
  49.     cin>>i;
  50.     if (i==0) {exit(0);}
  51.     if (i==1) {cls();check(h);menu(h);}
  52.     if (i==2) {cls();check(h);pozicija(h);menu(h);}
  53.     if (i==3) {cls();check(h);change(h);menu(h);}
  54.     if (i==4) {cls();newlist(h);}
  55.     if (i==5) {cls();trint(h);menu(h);}
  56.     else {cls();menu(h);}
  57. }
  58. void pozicija(node* h)
  59. {
  60.     int a,b,i;
  61.     cout<<"Pasirinkite i kuria pozicija norite iterpti skaiciu: ";cin>>a;
  62.     cout<<"Iveskite savo skaiciu: ";cin>>b;
  63.     node* temp;
  64.     node* l;
  65.     temp=h;
  66. for (i=2;i<=a && temp!=NULL;i++)
  67. {
  68.     l=temp;
  69.     temp=temp->link;
  70.     if (a==i)
  71.     {
  72.      node* temp1=new node;
  73.      temp1->data=b;
  74.      temp1->link=temp;
  75.      l->link=temp1;
  76.     }
  77. }
  78. cls();
  79.  
  80. }
  81. void newlist(node* h)
  82. {
  83.     int i,ii;
  84. node* l;
  85. if (h==NULL){
  86. node* temp=new node;
  87. l=temp;
  88. cout<<"Iveskite kiek bus skaiciu sarase: ";cin>>ii;
  89. for(i=0;i<ii;i++)
  90. {
  91. node* temp1=new node;
  92.     cout<<"iveskite "<<i+1<<" skaiciu: ";
  93.     cin>>temp1->data;
  94.     temp1->link=NULL;
  95.     if (i==0)
  96.     {
  97.         h=temp1;
  98.     }
  99.     else
  100.     {
  101.     l->link=temp1;
  102.     }
  103.     l=temp1;
  104. }
  105. cls();
  106. }else{cout<<"Sarasas jau egzistuoja, pirmiausia reikia ji istrinti\n\n}";}
  107. menu(h);
  108. }
  109. void change(node* h)
  110. {
  111. int a,b,c=0;
  112. cout<<"Iveskite skaiciu kuri norite pakeisti: ";cin>>a;
  113. cout<<"Iveskite skaiciu kuriuo norite pakeisti: ";cin>>b;
  114. cls();
  115.     int i=1;
  116.     node* temp;
  117.     temp=h;
  118.     if (h!=NULL)
  119.     {
  120.         while(temp->link!=NULL)
  121.         {
  122.             if (a==temp->data)
  123.             {
  124.             temp->data=b;
  125.             c=1;
  126.             cout<<"Rastas skaicius "<<a<<" jis pakeistas skaiciu: "<<temp->data<<endl;
  127.             }
  128.         temp=temp->link;
  129.         i++;
  130.         }
  131.     if (a==temp->data)
  132.     {
  133.     temp->data=b;
  134.     c=1;
  135.     cout<<"Rastas skaicius "<<a<<" jis pakeistas skaiciu: "<<temp->data<<endl;
  136.     }
  137.     }
  138.     if (h==NULL)
  139.     {
  140.     cls();
  141.     cout<<"Sarasas neegzistuoja"<<endl;
  142.     }
  143.     if (c==0)
  144.     {
  145.     cout<<"Skaicius nerastas...\n"<<endl;
  146.     }
  147. }
  148. int trint(node*& h)
  149. {
  150.     node* kitas;
  151.     node* temp=h;
  152.     temp=h;
  153.     while (temp->link!=NULL)
  154.     {
  155.         kitas=temp->link;
  156.         delete temp;
  157.         temp=kitas;
  158.     }
  159.     delete temp;
  160.     h=NULL;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement