Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.48 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.     node* trace;
  10. };
  11.  
  12. void check(node* h);
  13. void checkback(node* t);
  14. int menu(node* h,node* t);
  15. int pozicija(node*& h,node*& t);
  16. int prepozicija(node*& h,node*& t);
  17. int newlist(node*& h,node*& t);
  18. void change (node* h);
  19. int trint (node*& h,node*& t);
  20. void ieskot(node* h,node*t);
  21. void cls()
  22. {
  23. system ("CLS");
  24. }
  25. int main()
  26. {
  27. node* h=NULL;
  28. node* t=NULL;
  29. menu(h,t);
  30.     return 0;
  31. }
  32. void check(node* h)
  33. {
  34.     int i=1;
  35.     node* temp;
  36.     temp=h;
  37.     if (h!=NULL) {
  38. while(temp->link!=NULL){
  39.     cout<<"Saraso skaicius "<<i<<": "<<temp->data<<endl;
  40.     temp=temp->link;
  41.     i++;
  42. }
  43. cout<<"Saraso skaicius "<<i<<": "<<temp->data<<endl<<endl<<endl;
  44. }
  45. else
  46. {   cls();
  47.     cout<<"Sarasas neegzistuoja\n\n";
  48. }
  49. }
  50. int menu(node* h,node* t)
  51. {
  52. int i=0;
  53.     cout<<"pasirinkite funkcija: \n\nSkaiciu ne is saraso) Isjungti programa\n1) atspausdinti sarasa\n2) Perziureti saraso nuo galo\n3) iterpti nauja elementai nurodyta pozicija\n4) Iterpti skaiciu pries tam tikra pozicija\n5) iterpti elementa vietoj tam tikro elemento\n6) Sukurti nauja sarasa\n7) Istrinti dabartini sarasa"<<endl;
  54.     cin>>i;
  55.     if (i==0) {exit(0);}
  56.     if (i==1) {cls();check(h);menu(h,t);}
  57.     if (i==2) {cls();checkback(t);menu(h,t);}
  58.     if (i==3) {cls();check(h);pozicija(h,t);menu(h,t);}
  59.     if (i==4) {cls();check(h);prepozicija(h,t);menu(h,t);}
  60.     if (i==5) {cls();check(h);change(h);menu(h,t);}
  61.     if (i==6) {cls();newlist(h,t);menu(h,t);}
  62.     if (i==7) {cls();trint(h,t);menu(h,t);}
  63.     if (i==8) {cls();ieskot(h,t);menu(h,t);}
  64. }
  65. int pozicija(node*& h, node*& t)
  66. {
  67.     int a,b,i,tikr=0;
  68.     cout<<"Pasirinkite i kuria pozicija norite iterpti skaiciu: ";cin>>a;
  69.     cout<<"Iveskite savo skaiciu: ";cin>>b;
  70.     node* temp=h;
  71.     node* l;
  72. if (a==1)
  73. {
  74. node* temp1=new node;
  75. temp1->data=b;
  76. temp1->link=h;
  77. temp1->trace=NULL;
  78. h->trace=temp1;
  79. h=temp1;
  80. tikr=1;
  81. }
  82. for (i=2;i<=a && temp!=NULL;i++)
  83. {
  84.     l=temp;
  85.     temp=temp->link;
  86.     if (a==i)
  87.     {
  88.         if(temp==t)
  89.         {
  90.         node* temp1=new node;
  91.         temp1->data=b;
  92.         temp1->link=temp;
  93.         temp1->trace=l;
  94.         l->link=temp1;
  95.         temp->trace=temp1;
  96.         tikr=1;
  97.         }
  98.         else if(l==t)
  99.         {
  100.         node* temp1=new node;
  101.         temp1->data=b;
  102.         temp1->link=NULL;
  103.         temp1->trace=l;
  104.         t=temp1;
  105.         l->link=temp1;
  106.         tikr=1;
  107.         }
  108.         else
  109.         {
  110.         node* temp1=new node;
  111.         temp1->data=b;
  112.         temp1->link=temp;
  113.         temp1->trace=l;
  114.         l->link=temp1;
  115.         temp->trace=temp1;
  116.         tikr=1;
  117.         }
  118.     }
  119. }
  120. cls();
  121. if (tikr==1){cout<<"Skaicius "<<b<<" iterptas i pozicija "<<a<<endl;}
  122. else {cout<<"Skaiciaus iterpti nepavyko....\n";}
  123. }
  124. int prepozicija(node*& h, node*& t)
  125. {
  126.     int a,b,i,tikr=0;
  127.     cout<<"Pasirinkite pozicija pries kuria norite iterpti skaiciu: ";cin>>a;
  128.     cout<<"Iveskite savo skaiciu: ";cin>>b;
  129.     node* temp=h;
  130.     node* l;
  131. if (temp->data==a)
  132. {
  133.     cout<<"rastas skaicius";
  134. node* temp1=new node;
  135. temp1->data=b;
  136. temp1->link=h;
  137. temp1->trace=NULL;
  138. h->trace=temp1;
  139. h=temp1;
  140. tikr=1;
  141. }
  142. for (;temp->link!=NULL;)
  143. {
  144.     l=temp;
  145.     temp=temp->link;
  146.     if (temp->data==a)
  147.     {
  148.         if(temp==t)
  149.         {
  150.         node* temp1=new node;
  151.         temp1->data=b;
  152.         temp1->link=temp;
  153.         temp1->trace=l;
  154.         l->link=temp1;
  155.         temp->trace=temp1;
  156.         tikr=1;
  157.         }
  158.         else if(l==t)
  159.         {
  160.         node* temp1=new node;
  161.         temp1->data=b;
  162.         temp1->link=NULL;
  163.         temp1->trace=l;
  164.         t=temp1;
  165.         l->link=temp1;
  166.         tikr=1;
  167.         }
  168.         else
  169.         {
  170.         node* temp1=new node;
  171.         temp1->data=b;
  172.         temp1->link=temp;
  173.         temp1->trace=l;
  174.         l->link=temp1;
  175.         temp->trace=temp1;
  176.         tikr=1;
  177.         }
  178.     }
  179. }
  180. cls();
  181. if (tikr==1){cout<<"Skaicius "<<b<<" iterptas pries pozicija "<<a<<endl;}
  182. else {cout<<"Skaiciaus iterpti nepavyko....\n";}
  183. }
  184. int newlist(node*& h,node*& t)
  185. {
  186.     int i,ii;
  187. node* l;
  188. if (h==NULL){
  189. //node* temp=new node;
  190. node* l;
  191. cout<<"Iveskite kiek bus skaiciu sarase: ";cin>>ii;
  192. for(i=0;i<ii;i++)
  193. {
  194. node* temp1=new node;
  195.     cout<<"iveskite "<<i+1<<" skaiciu: ";
  196.     cin>>temp1->data;
  197.     temp1->link=NULL;
  198.     if (i==0)
  199.     {
  200.         h=temp1;
  201.         t=temp1;
  202.         temp1->trace=NULL;
  203.         temp1->link=NULL;
  204.     }
  205.     else
  206.     {
  207.         l->link=temp1;
  208.         temp1->trace=l;
  209.         t=temp1;
  210.     }
  211. l=temp1;
  212. }
  213. cls();
  214. }else{cout<<"Sarasas jau egzistuoja, pirmiausia reikia ji istrinti\n\n}";}
  215. }
  216. void change(node* h)
  217. {
  218. int a,b,c=0;
  219. cout<<"Iveskite skaiciu kuri norite pakeisti: ";cin>>a;
  220. cout<<"Iveskite skaiciu kuriuo norite pakeisti: ";cin>>b;
  221. cls();
  222.     int i=1;
  223.     node* temp;
  224.     temp=h;
  225.     if (h!=NULL)
  226.     {
  227.         while(temp->link!=NULL)
  228.         {
  229.             if (a==temp->data)
  230.             {
  231.             temp->data=b;
  232.             c=1;
  233.             cout<<"Rastas skaicius "<<a<<" jis pakeistas skaiciu: "<<temp->data<<endl;
  234.             }
  235.         temp=temp->link;
  236.         i++;
  237.         }
  238.     if (a==temp->data)
  239.     {
  240.     temp->data=b;
  241.     c=1;
  242.     cout<<"Rastas skaicius "<<a<<" jis pakeistas skaiciu: "<<temp->data<<endl;
  243.     }
  244.     }
  245.     if (h==NULL)
  246.     {
  247.     cls();
  248.     cout<<"Sarasas neegzistuoja"<<endl;
  249.     }
  250.     if (c==0)
  251.     {
  252.     cout<<"Skaicius nerastas...\n"<<endl;
  253.     }
  254. }
  255. int trint(node*& h,node*& t)
  256. {
  257.     node* kitas;
  258.     node* temp=h;
  259.     temp=h;
  260.     while (temp->link!=NULL)
  261.     {
  262.         kitas=temp->link;
  263.         delete temp;
  264.         temp=kitas;
  265.     }
  266.     delete temp;
  267.     h=NULL;
  268.     t=NULL;
  269. }
  270. void checkback(node* t)
  271. {
  272.     int i=1;
  273.     node* temp;
  274.     temp=t;
  275.     if (t!=NULL) {
  276. cout << "pirmas loop atliktas, i=" << i << endl;
  277. while(temp->trace!=NULL)
  278. {
  279. i++;
  280. temp=temp->trace;
  281. cout<<"pirmas loop atliktas, i="<<i<<endl;
  282. }
  283. temp=t;
  284. while (i!=0)
  285. {cout<<"Saraso skaicius "<<i<<": "<<temp->data<<endl;
  286.     temp=temp->trace;
  287. i--;
  288. }
  289. }
  290. else
  291. {   cls();
  292.     cout<<"Sarasas neegzistuoja\n\n";
  293. }
  294. }
  295. void ieskot(node* h,node*t)
  296. {
  297. node* temp;
  298. int a,i;
  299. cout<<"Iveskite skaiciu kurio norite ieskoti: ";
  300. cin>>a;
  301. cls();
  302. for (temp=h,i=1;temp!=t;temp=temp->link,i++)
  303. {
  304.     if(temp->data==a)
  305.     {
  306.         cout<<"Rastas #"<<i<<" skaicius "<<a<<endl;
  307.     }
  308.  
  309.  
  310.  
  311. }
  312. if(temp->data==a)
  313.     {
  314.         cout<<"Rastas #"<<i<<" skaicius "<<a<<endl;
  315.     }
  316. cout<<endl;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement