Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define max 5
  6.  
  7. typedef struct red{int niz[max]; int f; int r;}RED;
  8.  
  9. int isFull(RED* bafer)
  10. {
  11. return (bafer->r+1)%max== bafer->f;
  12. }
  13. int isEmpty(RED*bafer)
  14. {
  15. return bafer->f==-1;
  16. }
  17.  
  18. int dodaj(RED*bafer , int podatak)
  19. {
  20. if (isFull(bafer)) return 0; //printf("Bafer je pun");
  21. if (bafer->f==-1) bafer->f=0; //bafer->f++;
  22. bafer->r=(bafer->r+1)%max;
  23. bafer->niz[bafer->r]=podatak;
  24. return 1;
  25. }
  26.  
  27.  
  28. int brisi(RED*bafer, int *pom) //skidanje sa vrha
  29. {
  30. if (isEmpty(bafer)) return 0;
  31. *pom=bafer->niz[bafer->f];
  32. if (bafer->f == bafer->r)
  33. bafer->f=bafer->r=-1;
  34. else bafer->f=(bafer->f+1)%max;
  35. return 1;
  36.  
  37. }
  38.  
  39.  
  40.  
  41. int pisi(RED *bafer) {
  42. if (isEmpty(bafer))
  43. return 0;
  44. int i = bafer->f, j=0;
  45. if (bafer->f <= bafer->r)
  46. {
  47. while (i <= bafer->r)
  48. printf("%d. : %d\n ", ++j, bafer->niz[i++]);
  49. }
  50. else
  51. {
  52. while (i <= max - 1)
  53. printf("%d. : %d\n ", ++j, bafer->niz[i++]);
  54. i = 0;
  55. while (i <= bafer->r)
  56. printf("%d. :%d\n ", ++j, bafer->niz[i++]);
  57. }
  58. return 1;
  59. }
  60.  
  61.  
  62.  
  63. //
  64.  
  65. typedef struct cvor{ int podatak; struct cvor* sledeci;}CVOR;
  66.  
  67. void push(CVOR** tos, int podatak)
  68. {
  69. CVOR* novi=(CVOR*)malloc(sizeof(CVOR));
  70. novi->podatak=podatak;
  71. novi->sledeci=*tos;
  72. *tos=novi;
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. int pop(CVOR **tos, int *broj) {
  80. if (*tos == 0) return 0;
  81. CVOR *p = *tos;
  82. *broj = p->podatak;
  83. *tos = p->sledeci;
  84. free(p);
  85. return 1;
  86. }
  87.  
  88.  
  89. void pisi2(CVOR** tos)
  90. {
  91. int broj;
  92. while(pop(tos,&broj))
  93. printf("%d\n", broj);
  94. }
  95.  
  96.  
  97.  
  98. int main()
  99. {
  100. RED bafer; bafer.f = bafer.r = -1;
  101. char c;
  102. int broj;
  103. CVOR* tos=0;
  104.  
  105.  
  106. do {
  107. printf("===================================================\n");
  108. printf("Dodavanje[D], brisanje [B], prikaz [P], kraj [0]? ");
  109. scanf("\n%c", &c);
  110. if (c == 'D') {
  111. printf(" Unesite broj: ");
  112. scanf("%d", &broj);
  113. if (dodaj(&bafer, broj) == 0) printf(" Bafer je pun!\n");
  114. else push(&tos,broj);
  115. }
  116. else if (c == 'B') {
  117. if (brisi(&bafer, &broj)){
  118. printf(" Obrisan je broj: %d\n", broj);
  119.  
  120. }
  121. else printf(" Bafer je prazan!\n");
  122. }
  123. else if (c == 'P') {
  124. printf(" Sadrzaj bafera: ");
  125. if (pisi(&bafer) == 0)
  126. printf("\n Bafer je prazan!\n");
  127. else printf("\n");
  128. }
  129. else if (c != '0') printf(" Nepoznata opcija - '%c'.\n", c);
  130. } while (c != '0');
  131. printf("\nSadrzaj je:\n");
  132. while(pop(&tos,&broj))
  133. printf("%d ", broj);
  134. printf("KRAJ!\n"); return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement