Posted By


Bozidar on 11/11/12

Tagged


Statistics


Viewed 454 times
Favorited by 0 user(s)

Implemantacija_liste_pomocu_polja.h


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

Datoteka zaglavlja u kojoj se nalazi moja implementacija liste pomoću polja.
Datoteka sadži funkcije: InitL(L), FirstL(L), EndL(L),NextL(p,L),PreviousL(p,L), LocateL(x,L), InsertL(x,p,L), DeleteL(p,L), RetreiveL(p,L), DeleteAllL(L), br_unesenih_el(L), brisi_naziv(L), brisi_vrstu(L) i cijena_u_polje(L).


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. using namespace std;
  3. struct lista{
  4. int sifra[10000], dan[10000], mjesec[10000], godina[10000];
  5. float cijena[10000];
  6. char vrsta[10000][20], naziv[10000][20];
  7. int kursor;
  8. };
  9.  
  10. typedef struct lista list;
  11.  
  12. void InitL(list *l){
  13. l->kursor = 0;
  14. }
  15.  
  16. int FirstL(list *list){
  17. return 0;
  18. }
  19.  
  20. int EndL(list *l){
  21. return l->kursor;
  22. }
  23.  
  24. int NextL(int p, list *l)
  25. {
  26. if(p==--l->kursor)
  27. return l->kursor;
  28. if(p>=l->kursor)
  29. cout<<"Funkcija je nedefinirana!\n";
  30. else return(p+1);
  31. }
  32.  
  33. int PreviousL(int p, list *l){
  34. if(p==FirstL(l)){
  35. cout<<"Funkcija je nedefinirana!\n";
  36. return -1;
  37. }
  38. if(p==EndL(l))
  39. return --l->kursor;
  40. else
  41. return(p--);
  42. }
  43.  
  44. int LocateL(int x, list *l){
  45. for(int i=0; i<l->kursor;i++){
  46. if(x==l->sifra[i])
  47. return i;
  48. }
  49. return EndL(l);
  50. }
  51. int LocateL_naziv_vrsta(char unos[20], list *l, bool naziv_ili_vrsta){
  52. int broj = -1;
  53. if(naziv_ili_vrsta==true){
  54. for(int i=0; i<l->kursor;i++){
  55. if(strcmp(l->naziv[i],unos)==0)
  56. broj = i;
  57. }
  58. }
  59. else{
  60. for(int i=0; i<l->kursor;i++){
  61. if(strcmp(l->vrsta[i],unos)==0)
  62. broj = i;
  63. }
  64. }
  65. if(broj != -1)
  66. return broj;
  67. else return EndL(l);
  68. }
  69.  
  70. void InsertL(int dan, int mjesec, int godina, int sifra, char vrsta[20],char naziv[20],float cijena, int p, list *l){
  71. if(p<10000){
  72. for(int i=l->kursor;i>p;i--){
  73. l->sifra[i--] = l->sifra[i];
  74. l->vrsta[i--][20] = l->naziv[i][20];
  75. l->naziv[i--][20] = l->naziv[i][20];
  76. l->cijena[i--]=l->cijena[i];
  77. l->dan[i--] = l->dan[i];
  78. l->mjesec[i--] = l->mjesec[i];
  79. l->godina[i--] = l->godina[i];
  80. }
  81. l->sifra[p] = sifra;
  82. strcpy(l->vrsta[p],vrsta);
  83. strcpy(l->naziv[p],naziv);
  84. l->cijena[p]=cijena;
  85. l->dan[p] = dan;
  86. l->mjesec[p] = mjesec;
  87. l->godina[p] = godina;
  88. l->kursor++;
  89. }
  90. else cout<<"Lista je vec napunjena!\n";
  91. }
  92.  
  93. int DeleteL(int p, list *l){
  94. if ((p < l->kursor) && (p >= 0)) {
  95. for (int i=p; i<l->kursor; i++){
  96. l->sifra[i] = l->sifra[i+1];
  97. strcpy(l->vrsta[i],l->vrsta[i+1]);
  98. strcpy(l->naziv[i],l->naziv[i+1]);
  99. l->cijena[i]=l->cijena[i+1];
  100. l->dan[i] = l->dan[i+1];
  101. l->mjesec[i] = l->mjesec[i+1];
  102. l->godina[i] = l->godina[i+1];
  103. }
  104. l->kursor--;
  105. return l->kursor;
  106. }
  107. else {
  108. cout<<"Element liste ne postoji!";
  109. return 0;
  110. }
  111. }
  112.  
  113. void RetriveL(int p, list *l){
  114. if(p>=l->kursor){
  115. cout<<"Element liste ne postoji!";
  116. }
  117. else {
  118. cout<<"Sifra: "<<l->sifra[p]<<endl;
  119. cout<<"Vrsta zivotinje: "<<l->vrsta[p]<<endl;
  120. cout<<"Naziv zivotinje: "<<l->naziv[p]<<endl;
  121. cout<<"Cijena: "<<l->cijena[p]<<endl;
  122. cout<<"Datum dostave: "<<l->dan[p]<<"."<<l->mjesec[p]<<"."<<l->godina[p]<<"."<<endl;
  123. cout<<endl;
  124. }
  125. }
  126. void RetriveL_Naziv(int p, lista *l, bool naziv_vrsta){
  127. if(p>=l->kursor){
  128. cout<<"Element liste ne postoji!";
  129. }
  130. else{
  131. if(naziv_vrsta==true)
  132. cout<<l->sifra[p]<<"\t"<<l->naziv[p]<<endl;
  133. else cout<<l->sifra[p]<<"\t"<<l->vrsta[p]<<endl;
  134. }
  135. }
  136.  
  137. void DeleteAll(list *l){
  138. l->kursor = 0;
  139. }
  140. int br_unesenih_el(list *l){
  141. return l->kursor;
  142. }
  143. void od_zadnje_sifre(list *l, int *polje, int br_unesenih_el){
  144. for(int i=0; i<br_unesenih_el;i++)
  145. polje[i] = l->sifra[i];
  146. bool zamjena = true;
  147. for(int i=br_unesenih_el-1; i>0 && zamjena; i--){
  148. zamjena = false;
  149. for(int j = 0; j<i; j++)
  150. if(polje[j] > polje[j+1]){
  151. int pom = polje[j];
  152. polje[j] = polje[j+1];
  153. polje[j+1] = pom;
  154. zamjena = true;
  155. }
  156. }
  157. return;
  158. }
  159.  
  160. void RetriveL_datum(list *l){
  161. int n = 0;
  162. for(int p = 0; p<(l->kursor); p++){
  163. if(l->godina[p] > 2012){
  164. cout<<"Sifra: "<<l->sifra[p]<<endl;
  165. cout<<"Vrsta zivotinje: "<<l->vrsta[p]<<endl;
  166. cout<<"Naziv zivotinje: "<<l->naziv[p]<<endl;
  167. cout<<"Cijena: "<<l->cijena[p]<<endl;
  168. cout<<"Datum dostave: "<<l->dan[p]<<"."<<l->mjesec[p]<<"."<<l->godina[p]<<"."<<endl;
  169. cout<<endl;
  170. n++;
  171. }
  172. if(l->godina[p] == 2012){
  173. if(l->mjesec[p] > 9){
  174. cout<<"Sifra: "<<l->sifra[p]<<endl;
  175. cout<<"Vrsta zivotinje: "<<l->vrsta[p]<<endl;
  176. cout<<"Naziv zivotinje: "<<l->naziv[p]<<endl;
  177. cout<<"Cijena: "<<l->cijena[p]<<endl;
  178. cout<<"Datum dostave: "<<l->dan[p]<<"."<<l->mjesec[p]<<"."<<l->godina[p]<<"."<<endl;
  179. cout<<endl;
  180. n++;
  181. }
  182. }
  183. if(l->godina[p] == 2012){
  184. if(l->mjesec[p]==9){
  185. if(l->dan[p]>23){
  186. cout<<"Sifra: "<<l->sifra[p]<<endl;
  187. cout<<"Vrsta zivotinje: "<<l->vrsta[p]<<endl;
  188. cout<<"Naziv zivotinje: "<<l->naziv[p]<<endl;
  189. cout<<"Cijena: "<<l->cijena[p]<<endl;
  190. cout<<"Datum dostave: "<<l->dan[p]<<"."<<l->mjesec[p]<<"."<<l->godina[p]<<"."<<endl;
  191. cout<<endl;
  192. n++;
  193. }
  194. }
  195. }
  196. }
  197. cout<<"\nUkupan broj zivotinja dostavljenih nakon 23. 9. 2012. je: "<<n<<endl;
  198. }
  199. int brisi_naziv(list *l, bool naziv_ili_vrsta, int izlaz){
  200. char naziv[20];
  201. naziv_ili_vrsta = true;
  202. do{
  203. cout<<"Naziv: "; unos(naziv);
  204. if(LocateL_naziv_vrsta(naziv, l,naziv_ili_vrsta)==EndL(l)){
  205. cout<<"Zivotinja sa tim nazivom ne postoji!\n";
  206. cout<<"1. Ponovna pretraga\n";
  207. cout<<"2. Izlaz\n";
  208. cout<<"Vas odabir: "; cin>>izlaz;}
  209. }while(LocateL_naziv_vrsta(naziv, l, naziv_ili_vrsta)==EndL(l) && izlaz!=2);
  210. if(LocateL_naziv_vrsta(naziv, l, naziv_ili_vrsta)!=EndL(l)){ //ako u do-while stsnem 2 za izlaz a ne postoji ziv izbacit ce error..i zato sam stavio jos taj if
  211. DeleteL(LocateL_naziv_vrsta(naziv, l, naziv_ili_vrsta), l); //LocateL(1. lociram poziciju trazenog elem, 2. i jos samo proslijedim glavu liste)
  212. cout<<"Obrisano!\n";
  213. return 1;
  214. }
  215. else return 0;
  216.  
  217. }
  218. int brisi_vrstu(list *l, bool naziv_ili_vrsta, int izlaz){
  219. char vrsta[20];
  220. naziv_ili_vrsta = false;
  221. do{
  222. cout<<"Vrsta: "; unos(vrsta);
  223. if(LocateL_naziv_vrsta(vrsta, l, naziv_ili_vrsta)==EndL(l)){
  224. cout<<"Zivotinja s tom vrstom ne postoji!\n";
  225. cout<<"1. Ponovna pretraga\n";
  226. cout<<"2. Izlaz\n";
  227. cout<<"Vas odabir: "; cin>>izlaz;
  228. }
  229. }while(LocateL_naziv_vrsta(vrsta, l, naziv_ili_vrsta)==EndL(l) && izlaz!=2);
  230. if(LocateL_naziv_vrsta(vrsta, l, naziv_ili_vrsta)!=EndL(l)){ //ako u do-while stsnem 2 za izlaz a ne postoji trazena zivotinja izbacit ce error..i zato sam stavio jos taj if
  231. int broj = br_unesenih_el(l);
  232. do{
  233. DeleteL(LocateL_naziv_vrsta(vrsta, l, naziv_ili_vrsta), l); //LocateL(1. lociram poziciju trazenog elem, 2. i jos samo proslijedim glavu liste)
  234. }while(LocateL_naziv_vrsta(vrsta, l, naziv_ili_vrsta)!=EndL(l)); //do-while koristim zato sto zelim proc kroz listu vise puta(ako ima vise istih vrsta) i obrisat sve elemente s tom vrstom
  235. cout<<"Obrisano!\n";
  236. return 1;
  237. }
  238. else return 0;
  239. }
  240.  
  241. void cijena_u_polje(list *l, float *polje_cijena, float *polje_sifra,char nazivi[1000][20], int br_unesenih_el){
  242. for(int i=0; i<br_unesenih_el;i++){
  243. polje_cijena[i] = l->cijena[i];
  244. polje_sifra[i] = l->sifra[i];
  245. }
  246.  
  247. for(int i=0; i<br_unesenih_el;i++){
  248. for(int j=0; j<20; j++){
  249. nazivi[i][j] = l->naziv[i][j];
  250. }
  251.  
  252. }
  253.  
  254. //sort
  255. char temp[20];
  256. float pomocna = 0;
  257. float pomocna2 = 0;
  258. char hold[20];
  259. int pass;
  260. int i = 0;
  261. for ( pass = 1; pass < br_unesenih_el; pass++ ){
  262. for ( i = 0; i < br_unesenih_el - 1; i++ ){
  263. if (strncmp(nazivi[i],nazivi[i+1],br_unesenih_el) > 0){
  264. strcpy(hold, nazivi[i]);
  265. strcpy(nazivi[i], nazivi[i+1]);
  266. strcpy(nazivi[i+1], hold);
  267.  
  268. pomocna = polje_cijena[i];
  269. polje_cijena[i]=polje_cijena[i+1];
  270. polje_cijena[i+1]=pomocna;
  271.  
  272. pomocna2 = polje_sifra[i];
  273. polje_sifra[i]=polje_sifra[i+1];
  274. polje_sifra[i+1]=pomocna2;
  275. }
  276. }
  277. }
  278.  
  279.  
  280. }
  281. int LocateL_cijena(float x, int y, list *l){
  282. for(int i=0; i<l->kursor;i++){
  283. if(x==l->cijena[i] && y == l->sifra[i])
  284. return i;
  285. }
  286. return EndL(l);
  287. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.