lista_pokazivac.h


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

Implementacija liste pomocu pokazivaca


Copy this code and paste it in your HTML
  1. #ifndef _LISTA_POKAZIVAC_H_
  2. #define _LISTA_POKAZIVAC_H_
  3.  
  4. struct _tempList
  5. {
  6. int sifra;
  7. std::string naziv,vrsta,datum;
  8. float cijena;
  9. };
  10.  
  11. struct _list
  12. {
  13. _tempList a;
  14. _list *next;
  15. };
  16.  
  17. typedef _list *List;
  18. typedef _list Array;
  19.  
  20. _list *FirstL(_list *list)
  21. {
  22. return list->next;
  23. }
  24.  
  25. _list *EndL(_list *list)
  26. {
  27. _list *last = list;
  28. while (last->next)
  29. last = last->next;
  30. return last->next;
  31. }
  32.  
  33. _list *NextL(_list *temp,_list *list)
  34. {
  35. if (temp == EndL(list))
  36. return NULL;
  37. return temp->next;
  38. }
  39.  
  40. _list *PreviousL(_list *temp,_list *list)
  41. {
  42. if (temp == FirstL(list))
  43. return 0;
  44. _list *search = list->next;
  45. _list *previous = list;
  46. while (true)
  47. {
  48. if (search == temp)
  49. return previous;
  50. search = search->next;
  51. previous = previous->next;
  52. }
  53. }
  54.  
  55. _list *LocateL(int temp, _list *list)
  56. {
  57. _list *search = list->next;
  58. while (search)
  59. {
  60. if (search->a.sifra == temp)
  61. return search;
  62. search = search->next;
  63. }
  64. return EndL(list);
  65. }
  66.  
  67. _list *LocateL(float temp,_list *list)
  68. {
  69. _list *search = list->next;
  70. while (search)
  71. {
  72. if (search->a.cijena == temp)
  73. return search;
  74. search = search->next;
  75. }
  76. return EndL(list);
  77. }
  78.  
  79. _list *LocateL(std::string temp,_list *list)
  80. {
  81. _list *search = list->next;
  82. while (search)
  83. {
  84. if (search->a.datum == temp || search->a.naziv == temp || search->a.vrsta == temp )
  85. return search;
  86. search = search->next;
  87. }
  88. return EndL(list);
  89. }
  90.  
  91. bool InsertL(_tempList copyElement,_list *index,_list *list)
  92. {
  93. _list *newElement = new _list;
  94. _list *search = list->next;
  95. _list *previous = list;
  96. while (search != index)
  97. {
  98. search = search->next;
  99. previous = previous->next;
  100. }
  101. newElement->next = previous->next;
  102. previous->next=newElement;
  103. newElement->a.cijena = copyElement.cijena;
  104. newElement->a.datum = copyElement.datum;
  105. newElement->a.naziv = copyElement.naziv;
  106. newElement->a.sifra = copyElement.sifra;
  107. newElement->a.vrsta = copyElement.vrsta;
  108. return true;
  109. }
  110.  
  111. bool DeleteL(_list *index,_list *list)
  112. {
  113. _list *search = list->next;
  114. _list *previous = list;
  115. while (search)
  116. {
  117. if (search == EndL(list))
  118. return false;
  119. if (search == index)
  120. {
  121. previous->next = search->next;
  122. search->next = NULL;
  123. delete search;
  124. return true;
  125. }
  126. previous = search;
  127. search = search->next;
  128. }
  129. }
  130.  
  131. _tempList RetrieveL(_list *index, _list *list)
  132. { // ne vraca pokazivac jer u mainu hardkodirano _tempList a ne _tempList*
  133. _list *search = list;
  134. while (search)
  135. {
  136. if (search == index)
  137. return search->a;
  138. search = search->next;
  139. }
  140. }
  141.  
  142. bool DeleteAllL(_list *list)
  143. {
  144. if (!list->next)
  145. {
  146. delete list;
  147. return true;
  148. }
  149. _list *previous = list->next;
  150. _list *search = previous->next;
  151. while (search)
  152. {
  153. delete previous;
  154. previous = search;
  155. search = search->next;
  156. }
  157. delete previous;
  158. list->next = NULL;
  159. return true;
  160. }
  161.  
  162. bool InitL(_list *list)
  163. {
  164. list->next = NULL;
  165. return true;
  166. }
  167.  
  168. #endif

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.