Guest User

Untitled

a guest
Apr 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct tacka{
  5. int x;
  6. int y;
  7. int z;
  8. } TIP;
  9. typedef struct cvor_st
  10. {
  11. TIP inf;
  12. struct cvor_st *sledeci;
  13. } SCVOR;
  14.  
  15. void ispisi_stek(SCVOR *vrh)
  16. {
  17. if (vrh != NULL)
  18. {
  19. printf("element: (%i,%i,%i)\n", vrh->inf.x,vrh->inf.y,vrh->inf.z);
  20. ispisi_stek(vrh->sledeci);
  21. }
  22. }
  23.  
  24. void obrisi_stek(SCVOR *vrh)
  25. {
  26. if (vrh!= NULL)
  27. {
  28. printf("brisemo element: (%i,%i,%i)\n", vrh->inf.x,vrh->inf.y,vrh->inf.z);
  29. obrisi_stek(vrh->sledeci);
  30. free(vrh);
  31. }
  32. }
  33.  
  34. void push(TIP inf, SCVOR **vrh)
  35. {
  36. SCVOR *novi;
  37. novi = (SCVOR *)malloc(sizeof(SCVOR));
  38. novi->inf = inf;
  39. novi->sledeci = *vrh;
  40. *vrh = novi;
  41. }
  42.  
  43. TIP pop(SCVOR **vrh)
  44. {
  45. SCVOR *tmp;
  46. TIP pod,x;
  47.  
  48. x.x=-1; x.y=-1; x.z=-1;
  49.  
  50. if (*vrh == NULL)
  51. return x;
  52. /* pokupimo informaciju sa vrha steka */
  53. pod = (*vrh)->inf;
  54. /* zapamtimo element sa vrha steka da bismo
  55. ga obrisali nakon prevezivanja */
  56. tmp = *vrh;
  57. /* prevezemo (preskocimo) element sa vrha */
  58. *vrh = tmp->sledeci;
  59. /* obrisemo element sa vrha */
  60. free(tmp);
  61. return pod;
  62. }
  63.  
  64. TIP top(SCVOR *vrh)
  65. {
  66. TIP x;
  67. x.x=-1; x.y=-1; x.z=-1;
  68.  
  69. if (vrh == NULL)
  70. return x;
  71. return vrh->inf;
  72. }
  73.  
  74. int main(void)
  75. {
  76. /* vrh steka je lokalna promenljiva */
  77. SCVOR *vrh;
  78. TIP i;
  79. int odg;
  80.  
  81. /* inicijalizacija praznog steka*/
  82. vrh = NULL;
  83.  
  84. do {
  85.  
  86. printf("\n Opcije:\n");
  87. printf("\t1. Smestanje na stek - push.\n");
  88. printf("\t2. Skidanje sa steka - pop.\n");
  89. printf("\t3. Pristup prvom elementu - top.\n");
  90. printf("\t4. Prikaz sadrzaja celog steka.\n");
  91. printf("\t5. Kraj.\n");
  92. printf("\n\t>> ");
  93. scanf("%d", &odg);
  94.  
  95. switch( odg ) {
  96. case 1: printf("Unesite x: ");
  97. scanf("%d", &i.x);
  98. printf("Unesite y: ");
  99. scanf("%d", &i.y);
  100. printf("Unesite z: ");
  101. scanf("%d", &i.z);
  102. push(i, &vrh);
  103. break;
  104. case 2: i = pop(&vrh); /* pokupimo jedan element sa steka */
  105. printf("Skinuli sa steka: (%i,%i,%i)\n", i.x,i.y,i.z);
  106. break;
  107. case 3: printf("Na vrhu steka je (%i,%i,%i)\n", top(vrh).x,top(vrh).y,top(vrh).z); /* pogledamo sta je na vrhu steka */
  108. break;
  109. case 4: ispisi_stek(vrh); /* ispisemo sadrzaj stabla*/
  110. break;
  111. }
  112. } while ( odg!=5 );
  113.  
  114. /* obrisemo stek */
  115. obrisi_stek(vrh);
  116. vrh = NULL;
  117.  
  118. return 0;
  119. }
Add Comment
Please, Sign In to add comment