SP Zadatak 1 - implementacija pomocu pokazivaca


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

Zaglavlje lista_pokazivac.h


Copy this code and paste it in your HTML
  1. struct s_datum {
  2. unsigned int dan;
  3. unsigned int mjesec;
  4. unsigned int godina;
  5. };
  6.  
  7. struct s_zapis {
  8. short sifra;
  9. char vrsta[50];
  10. char naziv[50];
  11. float cijena;
  12. s_datum datum;
  13. };
  14.  
  15. struct lista {
  16. s_zapis zapis;
  17. lista *NEXT;
  18. };
  19. typedef lista *element;
  20.  
  21.  
  22. element EndL(lista *L) {
  23. lista *LAST = L;
  24. while (LAST->NEXT)
  25. LAST = LAST->NEXT;
  26.  
  27. return LAST;
  28. }
  29.  
  30. element FirstL(lista *L) {
  31. if (L->NEXT)
  32. return L;
  33. else
  34. return EndL(L);
  35. }
  36.  
  37. element NextL(element p, lista *L) {
  38. if (p->NEXT)
  39. return p->NEXT;
  40. else
  41. return EndL(L);
  42. }
  43.  
  44. element PreviousL(element p, lista *L) {
  45. if (p==L) return 0;
  46. lista *prev = L;
  47. while (prev->NEXT) {
  48. if (prev->NEXT == p)
  49. return prev;
  50. prev = prev->NEXT;
  51. }
  52. return FirstL(L);
  53. }
  54.  
  55. element LocateL(s_zapis X, lista *L) {
  56. lista *flow = L->NEXT;
  57. while (flow) {
  58. if (!strcmp(flow->zapis.vrsta, X.vrsta))
  59. return flow;
  60. else if (!strcmp(flow->zapis.naziv, X.naziv))
  61. return flow;
  62.  
  63. flow = flow->NEXT;
  64. }
  65.  
  66. return EndL(L);
  67. }
  68.  
  69. bool InsertL(s_zapis X, element p, lista *L) {
  70. if (L) {
  71. element novi = new lista;
  72. novi->zapis = X;
  73. novi->NEXT = p->NEXT;
  74. p->NEXT = novi;
  75. return true;
  76. }
  77. else
  78. return false;
  79. }
  80.  
  81. bool DeleteL(element p, lista *L) {
  82. element prev, flow;
  83. flow = p;
  84. if (p == FirstL(L)) {
  85. L->NEXT = flow->NEXT;
  86. delete flow;
  87. return true;
  88. }
  89. else if (p != FirstL(L)){
  90. prev = PreviousL(flow, L);
  91. prev->NEXT = flow->NEXT;
  92. delete flow;
  93. return true;
  94. }
  95. else return false;
  96. }
  97.  
  98. s_zapis RetrieveL(element p, lista *L) {
  99. return p->zapis;
  100. }
  101.  
  102. lista *InitL(lista *L) {
  103. L = new lista;
  104. L->NEXT = NULL;
  105. return L;
  106. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.