Implementacija liste pomocu pokazivaca


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



Copy this code and paste it in your HTML
  1. typedef int element;
  2.  
  3. struct list {
  4. elementtype values[100001];
  5. int cursor;
  6. };
  7.  
  8.  
  9. int FirstL(list *Li)
  10. {
  11. return(0);
  12. }
  13.  
  14. int EndL(list *Li)
  15. {
  16. return(Li->cursor);
  17. }
  18.  
  19.  
  20. element NextL(element p, list *Li)
  21. {
  22. if ((p>=Li->cursor) || (p<0)) {
  23. cout << "Nepostojeci element listeNEXT";
  24. system("pause");
  25. exit(0);
  26. }
  27. else
  28. return(p+1);
  29. }
  30.  
  31.  
  32. element PreviousL(element p, list *Li)
  33. {
  34. if ((p>Li->cursor) || (p<=0)) {
  35. cout << "Nepostojeci element listePREV";
  36. system("pause");
  37. exit(0);
  38. }
  39. else
  40. return(p-1);
  41. }
  42.  
  43.  
  44. element LocateL(elementtype x, list *Li)
  45. {
  46. int i;
  47. i=0;
  48. while ((i!= Li->cursor) && (Li->values[i]!=x)) i++;
  49. return(i);
  50. }
  51.  
  52.  
  53. void InsertL(elementtype x, element p, list *Li)
  54. {
  55. int i;
  56. if ((p<=Li->cursor) && (p>=0) && (Li->cursor<100005)) {
  57. for (i=Li->cursor; i>=p; i--)
  58. Li->values[i]=Li->values[i-1];
  59. Li->cursor++;
  60. Li->values[p]=x;
  61. }
  62. else {
  63. if(Li->cursor>=100005)
  64. cout << "Lista je puna";
  65. else
  66. cout << "Nepostojeci element listeINSERT";
  67. system("pause");
  68. exit(0);
  69. }
  70. }
  71.  
  72.  
  73. void DeleteL(element p, list *Li)
  74. {
  75. int i;
  76. if ((p<Li->cursor) && (p>=0)) {
  77. for (i=p; i<Li->cursor; i++)
  78. Li->values[i]=Li->values[i+1];
  79. (*Li).cursor--;
  80. }
  81. else {
  82. cout << "Nepostojeci element listeDEL";
  83. system("pause");
  84. exit(0);
  85. }
  86. }
  87.  
  88.  
  89. elementtype RetrieveL(element p, list *Li)
  90. {
  91. if ((p<Li->cursor) && (p>=0)) {
  92. return(Li->values[p]);
  93. }
  94. else {
  95. cout << "Nepostojeci element listeRETRIVER";
  96. system("pause");
  97. exit(0);
  98. }
  99. }
  100.  
  101.  
  102. void DeleteAllL(list *Li)
  103. {
  104. Li->cursor=0;
  105. }
  106.  
  107.  
  108. void InitL(list *Li)
  109. {
  110. Li->cursor=0;
  111. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.