Posted By

daja2 on 11/12/12


Tagged

listapokazivac


Versions (?)

lista_pokazivac.h


 / Published in: C++
 

Opis je implementacija liste pomocu pokazivaca

  1. //IMPLEMENTACIJA LISTE POMOCU POKAZIVACA
  2. struct podaci{
  3. int sifra;
  4. char naziv[30];
  5. char vrsta[30];
  6. float cijena;
  7. struct{
  8. int dan, mj, god;
  9. }dat;
  10. };
  11.  
  12. struct lis {
  13. podaci value;
  14. lis *next;
  15. };
  16.  
  17. typedef lis *element;
  18. typedef lis list;
  19.  
  20. void DeleteAllL(list* L){
  21. while(L->next){
  22. element toDelete = L->next;
  23. if(L->next){
  24. L->next = toDelete->next;
  25. delete toDelete;
  26. }
  27. }
  28. delete L;
  29. L = NULL;
  30. }
  31.  
  32.  
  33. list* InitL(list* L){
  34. if(L!=NULL)
  35. DeleteAllL(L);
  36. else{
  37. list *newList = new list;
  38. newList->next = NULL;
  39. return newList;
  40. }
  41. return NULL;
  42. }
  43.  
  44. element FirstL(list* L){
  45. return L;
  46. }
  47.  
  48. element EndL(list* L){
  49. element tren = L;
  50. while(tren->next){
  51. tren = tren->next;
  52. }
  53. return tren;
  54. }
  55.  
  56. element NextL(element p, list *L){
  57. if(p!=NULL){
  58. if(p==EndL(L)){
  59. return NULL;
  60. }
  61. return p->next;
  62. }
  63. return EndL(L);
  64. }
  65.  
  66. element PreviousL(element p, list *L){
  67. element tren = L;
  68. while(tren){
  69. if(tren->next == p )
  70. return tren;
  71. tren = tren->next;
  72. }
  73. return NULL;
  74. }
  75.  
  76. element LocateL(podaci el, list* L){
  77. element tren = L;
  78. while(tren->next){
  79. if(tren->next->value.sifra == el.sifra)
  80. return tren;
  81. tren = tren->next;
  82. }
  83. return NULL;
  84. }
  85.  
  86. void InsertL(podaci el, element p, list* L){
  87. if(p==NULL)
  88. return;
  89. else{
  90. element novi = new lis;
  91. novi->value = el;
  92. novi->next = p->next;
  93. p->next = novi;
  94. }
  95. }
  96.  
  97. void DeleteL(element p, list* L){
  98. element toDelete = p->next;
  99. if(toDelete){
  100. p->next = toDelete->next;
  101. delete toDelete;
  102. }
  103. }
  104.  
  105. podaci RetrieveL(element p, list* L){
  106. return p->next->value;
  107. }

Report this snippet  

You need to login to post a comment.