Posted By


dmikulin on 11/12/12

Tagged


Statistics


Viewed 309 times
Favorited by 0 user(s)

Related snippets


Zadatak1_strukture


/ Published in: C++
Save to your folder(s)

implementacija liste pomoću pokazivača


Copy this code and paste it in your HTML
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. //#include "lista_pokazivaci.h"
  7. #include "lista_polje.h"
  8.  
  9.  
  10. bool prazno( list* L){
  11. return FirstL(L) == EndL(L);
  12. }
  13.  
  14. int Novi_unos(list *L){
  15. int pocetno=1;
  16. int sifra = 0;
  17. if(prazno(L)) sifra = pocetno;
  18. else sifra = RetrieveL(PreviousL(EndL(L),L),L).sifra+1;
  19.  
  20.  
  21. zivotinje unos;
  22. cout <<"Sifra: " << sifra << endl;
  23. unos.sifra = sifra;
  24. cout << "Naziv: "; cin>>unos.naziv;
  25. cout << "Vrsta: "; cin>>unos.vrsta;
  26. cout << "Cijena: "; cin >> unos.cijena;
  27.  
  28.  
  29. if(unos.cijena < 0) return 0;
  30. cout << "datum dostave)" << endl;
  31. cout << "Dan: "; cin >> unos.datum.dan;
  32. if(unos.datum.dan<1 || unos.datum.dan>31) return 0;
  33. cout << "Mjesec: "; cin >> unos.datum.mj;
  34. if(unos.datum.mj<1 || unos.datum.mj>12) return 0;
  35. cout << "Godina: "; cin >> unos.datum.god;
  36. if(unos.datum.god<1980) return 0;
  37.  
  38. InsertL(unos,EndL(L),L);
  39. return 1;
  40. }
  41.  
  42. void ispis(list *L){
  43.  
  44. elem end = EndL(L);
  45. while(end != FirstL(L)){
  46.  
  47. zivotinje ispis = RetrieveL(PreviousL(end,L),L);
  48.  
  49. cout << "Sifra: " << ispis.sifra << endl;
  50. cout << "Naziv: " << ispis.naziv << endl;
  51. cout << "Vrsta: " << ispis.vrsta << endl;
  52. cout << "Cijena: " << ispis.cijena << endl;
  53. cout << "datum dostave: " << ispis.datum.dan<<"." <<ispis.datum.mj << "."
  54. <<ispis.datum.god<<"."<<endl;
  55. cout <<endl;
  56. end = PreviousL(end,L);
  57. }
  58. }
  59.  
  60. void obrisi_prije( list* L){
  61.  
  62. if(prazno(L)){
  63. cout << endl << "Nema životinja unesenih poslje" << endl;
  64. return;
  65. }
  66.  
  67. int broj = 0;
  68. elem trenutno = FirstL(L);
  69.  
  70. struct{
  71. int dan,mj,god;
  72. }dost;
  73.  
  74. while(trenutno != EndL(L)){
  75. zivotinje prem = RetrieveL(trenutno,L);
  76. dost.dan = prem.datum.dan;
  77. dost.mj = prem.datum.mj;
  78. dost.god = prem.datum.god;
  79.  
  80. if((dost.god>2012) ||
  81. ((dost.god==2012)&&(dost.mj>9)) ||
  82. ((dost.god==2012)&&(dost.mj==9)) && (dost.dan>23)){
  83. broj++;
  84. cout << "Sifra: " << prem.sifra << endl;
  85. cout << "Naziv: " << prem.naziv << endl;
  86. cout << "Vrsta: " << prem.vrsta << endl;
  87. cout << "Cijena: " << prem.cijena << endl;
  88. cout << "datum dostave: " << prem.datum.dan<<"."
  89. <<prem.datum.mj << "."
  90. <<prem.datum.god<<"."<<endl;
  91. cout << endl;
  92. }
  93. trenutno = NextL(trenutno,L);
  94. }
  95. cout << endl << "Broj pronadjenih zivotinja: " << broj << endl;
  96. }
  97.  
  98. int Brisanje_naziv(list* L){
  99. char naziv[30];
  100.  
  101. cin.ignore();
  102. cout << "Unesite naziv: "; cin>>naziv;
  103.  
  104. if(prazno(L)) return 0;
  105.  
  106. elem trenutno = FirstL(L);
  107. bool postoji = false;
  108. while(trenutno != EndL(L)){
  109. zivotinje ziv = RetrieveL(trenutno,L);
  110. if(strcmp(ziv.naziv,naziv)==0){
  111. postoji = true;
  112. break;
  113. }
  114. trenutno = NextL(trenutno,L);
  115. }
  116.  
  117. if(postoji){
  118. DeleteL(trenutno,L);
  119. }
  120. else
  121. return 0;
  122.  
  123. return 1;
  124. }
  125.  
  126. int Brisanje_vrsta(list* L){
  127. char vrsta[30];
  128. cin.ignore();
  129. cout << "Upisite vrstu: ";
  130. cin>>vrsta;
  131.  
  132. elem trenutno = FirstL(L);
  133. bool postoji = false;
  134. while(trenutno != EndL(L)){
  135. zivotinje ziv = RetrieveL(trenutno,L);
  136. if(strcmp(ziv.vrsta,vrsta)==0){
  137. postoji = true;
  138. DeleteL(trenutno,L);
  139. }
  140. trenutno = NextL(trenutno,L);
  141. }
  142. if(!postoji)
  143. return 0;
  144. return 1;
  145. }
  146.  
  147. void spajanje(zivotinje polje[], int prvi, int sredina, int drugi){
  148. int i=prvi;
  149. int j=sredina+1;
  150. int k=0;
  151. zivotinje *b=new zivotinje[drugi-prvi+1];
  152.  
  153. while(i<=sredina && j<=drugi){
  154. if(polje[i].cijena>polje[j].cijena) b[k++]=polje[i++];
  155. else if(polje[i].cijena<polje[j].cijena) b[k++]=polje[j++];
  156. if(polje[i].cijena==polje[j].cijena){
  157. if(strcmp(polje[i].naziv,polje[j].naziv)==1)
  158. b[k++]=polje[i++];
  159. else
  160. b[k++]=polje[j++];
  161. }
  162. }
  163. while(j<=drugi) b[k++]=polje[j++];
  164. while(i<=sredina) b[k++]=polje[i++];
  165. for(int c=0;c<=drugi-prvi;c++) polje[c+prvi]=b[c];
  166. delete []b;
  167. }
  168.  
  169.  
  170.  
  171.  
  172. void Msort(zivotinje polje[], int prvi, int drugi){
  173. if(prvi<drugi){
  174. int sredina=(prvi+drugi)/2;
  175. Msort(polje,prvi,sredina);
  176. Msort(polje,sredina+1,drugi);
  177. spajanje(polje,prvi,sredina,drugi);
  178. }
  179. }
  180. void Msort(zivotinje polje[],int vel){
  181. Msort(polje,0,vel-1);
  182. }
  183.  
  184. void p6(list *gl){
  185. if(FirstL(gl)!=EndL(gl)){
  186. int broj=0;
  187. elem poz=FirstL(gl);
  188. while(false==(!true)){
  189. broj++;
  190. poz=NextL(poz,gl);
  191. if(poz==EndL(gl))
  192. break;
  193. }
  194. poz=FirstL(gl);
  195. zivotinje *sort=new zivotinje[broj];
  196. int mj=0;
  197. while(false==(!true)){
  198. sort[mj]=RetrieveL(poz,gl);
  199. mj++;
  200. poz=NextL(poz,gl);
  201. if(poz==EndL(gl))
  202. break;
  203. }
  204.  
  205. Msort(sort,broj);
  206. for(int i=0;i<broj;i++){
  207. cout<<endl<<"Sifra: "<<sort[i].sifra;
  208. cout<<endl<<"Cijena: "<<sort[i].cijena;
  209. cout<<endl<<"Naziv: "<<sort[i].naziv;
  210. cout<<endl<<"Vrsta: "<<sort[i].vrsta;
  211. cout<<endl<<"datum dostave: "
  212. <<sort[i].datum.dan<<"."
  213. <<sort[i].datum.mj<<"."
  214. <<sort[i].datum.god<<"."<<endl;
  215. }
  216. delete []sort;
  217. }
  218. else
  219. cout<<endl<<"Prazna lista!"<<endl;
  220. }
  221.  
  222. int main(){
  223.  
  224. list* L = NULL;
  225. L = InitL(L);
  226. int odabir = 0;
  227.  
  228. do{
  229. cout << endl;
  230. cout << "1. Dodaj zapis" << endl;
  231. cout << "2. Ispisi listu" << endl;
  232. cout << "3. Ispisi zivotinje dostavljene poslje 23. rujna 2012" << endl;
  233. cout << "4. Brisi prema nazivu zivotinje" << endl;
  234. cout << "5. Brisi prema vrsti zivotinje" << endl;
  235. cout << "6. Silazno sortiranje" << endl;
  236. cout << "9. Izlaz" << endl;
  237. cout << "-----------------------------" << endl;
  238. cin >> odabir;
  239. system("cls");
  240. switch(odabir){
  241. case 1:
  242. if(!Novi_unos(L)) cout << "Pogresno uneseni zivotinje!" << endl;
  243. break;
  244. case 2:
  245. ispis(L);
  246. break;
  247. case 3:
  248. obrisi_prije(L);
  249. break;
  250. case 4:
  251. if(Brisanje_naziv(L)) cout << "Zivotinja obrisana." << endl;
  252. else cout << "Pogresni naziv." << endl;
  253. break;
  254. case 5:
  255. if(Brisanje_vrsta(L)) cout << "Vrsta obrisana!" << endl;
  256. else cout << "Pogresna vrsta!" << endl;
  257. break;
  258. case 6:
  259. p6(L);
  260. break;
  261. case 9:
  262. break;
  263. default:
  264. cout << "Pogrešan unos!" << endl;
  265. }
  266. }while(odabir != 9);
  267.  
  268. DeleteAllL(L);
  269. system("pause");
  270. return 0;
  271. }

URL: lista

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.