Implementacija liste pomocu polja


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



Copy this code and paste it in your HTML
  1. struct list {
  2. elementtype value;
  3. struct list *next;
  4. };
  5.  
  6.  
  7. typedef list *element;
  8.  
  9. element FirstL(list *L)
  10. {
  11. return(L->next);
  12. }
  13.  
  14.  
  15. element EndL(list *L)
  16. {
  17. list *l;
  18. l=L;
  19. while (l->next != NULL) {
  20. l=l->next;
  21. }
  22. return(l);
  23. }
  24.  
  25.  
  26. element NextL(element p, list *L)
  27. {
  28. if (p->next == NULL) {
  29. cout << "Nepostojeci element NEXT";
  30. exit(0);
  31. }
  32. else {
  33. list *l;
  34. l=p->next;
  35. return(l);
  36. }
  37. }
  38.  
  39.  
  40. element PreviousL(element p, list *L)
  41. {
  42. list *l;
  43. l=L;
  44. while ((l->next != NULL) && (l->next != p))
  45. l=l->next;
  46. if (l->next != NULL)
  47. return(l);
  48. else {
  49. cout << "Nepostojeci element PREV";
  50. system("pause");
  51. exit(0);
  52. }
  53. }
  54.  
  55.  
  56. element LocateL(elementtype x, list *L)
  57. {
  58. list *l;
  59. l=L;
  60. while ((l->next != NULL) && (l->next->value != x))
  61. l=l->next;
  62. return(l);
  63. }
  64.  
  65.  
  66. void InsertL(elementtype x, element p, list *L)
  67. {
  68. list *l;
  69. l=new list;
  70. l->next=p->next;
  71. p->next=l;
  72. l->value = p->value;
  73. p->value = x;
  74.  
  75. }
  76.  
  77.  
  78. void DeleteL(element p, list *L)
  79. {
  80. element l;
  81. p=PreviousL(p,L);
  82. if (p->next != NULL) {
  83. l=p->next;
  84. p->next=p->next->next;
  85. delete l;
  86. }
  87. else {
  88. cout << "Nepostojeci element DELETE";
  89. system("pause");
  90. exit(0);
  91. }
  92. }
  93.  
  94.  
  95. elementtype RetrieveL(element p, list *L)
  96. {
  97. if (p->next != NULL) {
  98. elementtype x;
  99. x=p->value;
  100. return(x);
  101. }
  102. else {
  103. cout << "Nepostojeci element RETRIVER";
  104. system("pause");
  105. exit(0);
  106. }
  107. }
  108.  
  109.  
  110. void DeleteAllL(list *L)
  111. {
  112. element l;
  113. while (L->next != NULL) {
  114. l=L->next;
  115. L->next = L->next->next;
  116. delete l;
  117. }
  118. }
  119.  
  120.  
  121. void InitL(list *L)
  122. {
  123. list *l;
  124. l = new list;
  125. L->next=l;
  126. l->next=NULL;
  127.  
  128. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.