Advertisement
Guest User

Untitled

a guest
May 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<time.h>
  3. #include<cstdlib>
  4. #include<windows.h>
  5.  
  6. using namespace std;
  7. struct Element
  8. {
  9. int el;
  10. struct Element *next;
  11. };
  12.  
  13. struct Element *head;
  14. struct Element *tail;
  15.  
  16. void dodaj(int el,int n,int hyh);
  17. void usun(int n);
  18. bool czyPusty();
  19. void wyswietl();
  20. void srednia(int *pom);
  21. void usunL();
  22.  
  23. int main()
  24. {
  25. srand(time(NULL));
  26. int el, pom;
  27. pom=1;
  28. el = rand()%9+1;
  29. int wybor;
  30. head = NULL;
  31. tail = NULL;
  32. int nr;
  33. cout<<"Masz do wyboru:\n";
  34. cout<<"1.Sprawdzenie czy lista jest pusta\n";
  35. cout<<"2.Dodanie elementu na koniec listy\n";
  36. cout<<"3.Dodanie elementu na poczatek listy\n";
  37. cout<<"4.Dodanie elementu na pozycjê przed elementem o podanym numerze\n";
  38. cout<<"5.Usuniecie elementu z konca listy\n";
  39. cout<<"6.Usuniecie elementu z poczatku listy\n";
  40. cout<<"7.Usuniecie elementu znajdujacego siê na okreslonej pozycji\n";
  41. cout<<"8.Pobranie pierwszego elementu z listy\n";
  42. cout<<"9.Pobranie ostatniego elementu z listy\n";
  43. cout<<"10.Policzenie sredniej arytmetycznej elementww w liscie\n";
  44. cout<<"11.Wyswietlenie calej listy\n";
  45. cout<<"12.Usuniecie calej listy wraz ze zwolnieniem pamieci\n";
  46. cout<<"13.EXIT!\n";
  47. while(wybor!=13)
  48. {
  49. cout<<"\nPodaj ktoras opcje:";
  50. cin>>wybor;
  51.  
  52. switch(wybor)
  53. {
  54. case 1:
  55. if(czyPusty()==true)
  56. {
  57. cout<<"Lista jest pusta!\n";
  58. }
  59. else
  60. cout<<"Lista nie jest pusta!\n";
  61. break;
  62. case 2:
  63.  
  64. cout<<"Liczba: ";
  65. cin>>nr;
  66. dodaj(nr,pom,pom);
  67. pom++;
  68. wyswietl();
  69. break;
  70.  
  71. case 3:
  72.  
  73. cout<<"Liczba: ";
  74. cin>>nr;
  75. dodaj(nr,1,pom);
  76. pom++;
  77. wyswietl();
  78. break;
  79. case 4:
  80. cout<<"Liczba: ";
  81. cin>>nr;
  82. int pozycja;
  83. cout<<"Podaj pozycje: ";
  84. cin>>pozycja;
  85. if(pozycja<1 || pozycja>pom)
  86. if(pom==1)
  87. cout<<"Lista jest pusta!n";
  88. else
  89. cout<<"Mozesz podac tylko pozycje od 1 do "<<pom<<"\n";
  90. else
  91. {
  92. dodaj(nr, pozycja,pom);
  93. pom++;
  94. wyswietl();
  95. }
  96. break;
  97. case 5:
  98. if(czyPusty()==true)
  99. {
  100. cout<<"Lista jest pusta, nie ma nic do usuwania!\n";
  101. }
  102. else
  103. {
  104. pom--;
  105. usun(pom);
  106. wyswietl();
  107. }
  108. break;
  109. case 6:
  110. if(czyPusty()==true)
  111. cout<<"\nLista jest pusta, nie da sie usunac elementu.\n";
  112. else1
  113. {
  114. cout<<"Przed usunieciem: \n";
  115. wyswietl();
  116. usun(1);
  117. pom--;
  118. cout<<"\nPo: \n";
  119. wyswietl();
  120. }
  121. break;
  122. case 7:
  123. if(czyPusty()==true)
  124. cout<<"\nLista jest pusta, nie da sie usunac elementu.\n";
  125. else
  126. {
  127. cout<<"Przed usunieciem: \n";
  128. wyswietl();
  129. cout<<"\nPodaj pozycje: ";
  130. cin>>pozycja;
  131. if(pozycja<1 || pozycja>pom-1)
  132. {
  133. cout<<"Mozesz podac tylko pozycje od 1 do "<<pom-1<<endl;
  134. }
  135. else
  136. {
  137. usun(pozycja);
  138. pom--;
  139. cout<<"\nPo: \n";
  140. wyswietl();
  141. }
  142. }
  143. break;
  144. case 8:
  145. if(head!=NULL)
  146. cout<<"Pierwszy element w liscie: "<<head->el<<endl;
  147. else
  148. cout<<"Lista jest pusta\n";
  149. break;
  150. case 9:
  151. if(tail!=NULL)
  152. cout<<"Ostatni element w liscie: "<<tail->el<<endl;
  153. else
  154. cout<<"Lista jest pusta\n";
  155.  
  156. break;
  157. case 10:
  158. srednia(&pom);
  159. break;
  160.  
  161. case 11:
  162. wyswietl();
  163. break;
  164. case 12:
  165. usunL();
  166. pom=1;
  167. break;
  168. case 13:
  169. exit(1);
  170. break;
  171. }
  172. }
  173. delete head;
  174. return 0;
  175. }
  176. bool czyPusty()
  177. {
  178. if(head==NULL)
  179. {
  180. return true;
  181. }
  182. else
  183. {
  184. return false;
  185. }
  186. }
  187. void dodaj(int el,int n,int hyh)
  188. {
  189. Element *he=new Element();
  190. he->el=el;
  191. he->next=NULL;
  192. if(n==1)
  193. {
  194. he->next=head;
  195. head=he;
  196. if(tail==NULL)
  197. {
  198. tail=head;
  199. }
  200. return;
  201. }
  202. Element *he2 = head;
  203. for(int i=0; i<n-2; i++)
  204. {
  205. he2=he2->next;
  206. }
  207. he->next=he2->next;
  208. he2->next=he;
  209. if(n==hyh)
  210. {
  211. tail=he;
  212. }
  213.  
  214. }
  215. void usun(int n)
  216. {
  217. Element *he = head;
  218. if(n == 1)
  219. {
  220. head = he->next;
  221. delete he;
  222.  
  223. }
  224. else
  225. {
  226. for(int i=0; i<n-2; i++)
  227. he = he->next;
  228. Element *he2 = he->next;
  229. he->next = he2->next;
  230. tail=he;
  231. delete he2;
  232.  
  233. }
  234.  
  235. }
  236. void wyswietl()
  237. {
  238. Element *he = new Element();
  239. he = head;
  240. cout<<"\nLista: ";
  241. while(he != NULL)
  242. {
  243. cout<<he->el<<" ";
  244. he = he->next;
  245. }
  246. cout<<"\n\n";
  247. }
  248. void srednia(int *pom)
  249. {
  250. float suma=0;
  251. int ile=*pom-1;
  252. Element *he = new Element();
  253. he = head;
  254. cout<<"\nLista: ";
  255. while(he != NULL)
  256. {
  257. suma+=he->el;
  258. he = he->next;
  259. }
  260. float sredniaa = suma/ile;
  261. cout<<"\nSrednia arytmetyczna wynosi: "<<sredniaa<<endl;
  262. }
  263. void usunL()
  264. {
  265. Element *he = new Element();
  266. while(head!=NULL)
  267. {
  268. he = head;
  269. head = he->next;
  270. delete he;
  271.  
  272. }
  273. tail=NULL;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement