Advertisement
Guest User

Untitled

a guest
May 24th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. struct elementas
  7. {
  8. int data;
  9. elementas * next;
  10. };
  11.  
  12. elementas * temp;
  13. elementas * head;
  14. elementas * tail;
  15.  
  16.  
  17. void sukurimas(int &n);
  18. void spausdinimas(int n);
  19. void iterpimaspries(int &n);
  20. void pozicija(int &n);
  21. void pagalpozicija(int &n);
  22. void trint(int &n);
  23. void iterpimas(int &n);
  24. void posk(int &n);
  25.  
  26.  
  27. int main()
  28. {
  29. int * p,i,n=0;
  30. //head = NULL;
  31. int choice;
  32. bool meniu = true;
  33. while (meniu != false)
  34. {
  35. cout << "*******************************\n";
  36. cout << "1. Sukurti tiesini vienakrypti sarasa"<<endl;
  37. cout << "2. Atspausdinti tiesini vienakrypti sarasa"<<endl;
  38. cout << "3. Iterpia nauja elementa i nurodyta tiesinio saraso pozicija "<<endl;
  39. cout << "4. Iterpia elementa pries tam tikra sarase egzistuojanti elementa"<<endl;
  40. cout << "5. Istrina tiesini sarasa"<<endl;
  41. cout << "6. Istrina pasirinkta tiesinio saraso elementa"<<endl;
  42. cout << "7. UZDARYTI"<<endl;
  43. cout <<"Pasirinkite : "<<endl;
  44.  
  45. cin >> choice;
  46.  
  47. switch (choice)
  48. {
  49. case 1:
  50. sukurimas(n);
  51. break;
  52. case 2:
  53. spausdinimas(n);
  54. break;
  55. case 3:
  56. pozicija(n);
  57. break;
  58. case 4:
  59. iterpimas(n);
  60. break;
  61. case 5:
  62. trint(n);
  63. break;
  64. case 6:
  65. pagalpozicija(n);
  66. break;
  67. case 7:
  68. meniu = false;
  69. break;
  70. default:
  71. cout <<" Neegzistuoja "<<endl;
  72. break;
  73. cout << "PASIRINKITE: .\n";
  74. cin >> choice;
  75. break;
  76. }
  77. }
  78. return 0;
  79. }
  80. void sukurimas(int &n)
  81. {
  82. cout <<"Keliu elementu bus vienakryptis tiesinis sarasas"<<endl;
  83. cin >> n;
  84. head = new elementas;
  85. cout<<"IVESKITE " << 1 <<" ELEMENTA"<<endl;;
  86. cin>>head->data;
  87. head->next = nullptr;
  88. temp = head;
  89. tail = head;
  90.  
  91. for(int i = 1; i< n; i++)
  92. {
  93. temp = new elementas;
  94. cout <<"IVESKITE "<<i + 1 <<" ELEMENTA"<<endl;
  95. cin>>temp->data;
  96. temp->next=nullptr;
  97. tail->next = temp;
  98. tail=temp;
  99. }
  100. }
  101. void spausdinimas(int n)
  102. {
  103.  
  104. cout << "SARASO ELEMENTAI: " << endl;
  105. temp = head;
  106. while (temp != nullptr)
  107. {
  108. cout << temp->data << endl;
  109. cout << "***************" << endl;
  110. temp = temp->next;
  111. }
  112.  
  113. }
  114.  
  115. void iterpimaspries(int &n)
  116. {
  117. tail = new elementas;
  118. cout << "IVESKITE ELEMENTA: ";
  119. cin >> tail -> data;
  120. tail ->next = head;
  121. head = tail;
  122. n++;
  123. }
  124.  
  125. void pozicija(int &n)
  126. {
  127. int p;
  128. temp = head;
  129. cout << "IVESKITE POZICIJA: ";
  130. cin >> p;
  131. if (p > n || p < 1)
  132. cout << "NEGALIMA ITERPTI" << endl;
  133. else if (p == 1)
  134. iterpimaspries(n);
  135. else
  136. {
  137. tail = new elementas;
  138. cout << "IVESKITE ELEMENTA: ";
  139. cin >> tail -> data;
  140. for (int i = 1; i < p-1; i++)
  141. {
  142. temp = temp ->next;
  143. }
  144.  
  145. tail -> next = temp -> next;
  146. temp -> next = tail;
  147. n++;
  148. }
  149. }
  150.  
  151. void trint(int &n)
  152. {
  153. elementas * temp2=head;
  154. while(head != nullptr)
  155. {
  156. temp2= head;
  157. head = temp2->next;
  158. delete temp2;
  159. cout<<"Istrintas "<<temp2->data<<endl;
  160. n--;
  161. }
  162. cout << "SARASAS ISTRINTAS" << endl;
  163. }
  164.  
  165.  
  166. void iterpimas(int &n)
  167. {
  168.  
  169. int p;
  170. cout <<"PASIRINKITE ELEMENTA:"<<endl;
  171. cin>>p;
  172. temp = head;
  173. for(int i = 0 ; i<n-1; i++)
  174. {
  175. if(temp->data==p)
  176. {
  177. p = i+1;//
  178. break;
  179. }
  180. }
  181. if (p > n || p < 1)
  182. cout << "NEGALIMA ITERPTI!" << endl;
  183. else if (p == 1)
  184. iterpimaspries(n);
  185. else
  186. {
  187.  
  188. temp = new elementas;
  189. elementas * temp2=head;
  190. cout << "IVESKITE ELEMENTA: ";
  191. cin >> temp -> data;
  192. for (int i = 1; i < p-1; i++)
  193. {
  194. temp2= temp2 ->next;
  195. }
  196.  
  197. temp -> next = temp -> next;
  198. // temp -> next = temp; pagal pozicija / ir iterpti pagal pozicija
  199. n++;
  200. }
  201. }
  202. void pagalpozicija(int &n)
  203. {
  204. int sk, x = 0;
  205. elementas * temp2;
  206. cout << "IVESKITE KURI SKAICIU TRINTI: ";
  207. cin >> sk;
  208.  
  209. if ( sk == head->data)
  210. {
  211. trint(n);
  212. }
  213. else
  214. {
  215. temp2 = head;
  216. temp = temp2 -> next;
  217.  
  218. for (int i = 1; i <= n; i++)
  219. {
  220. if (temp -> data == sk )
  221. {
  222. delete temp;
  223. temp2 -> next = temp -> next;
  224. n--;
  225. x = 1;
  226. break;
  227. }
  228. else
  229. {
  230. cout << "NERA TOKIO SKAICIAUS" << endl;
  231. break;
  232. }
  233.  
  234. temp = temp -> next;
  235. temp2 = temp2 -> next;
  236. }
  237.  
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement