SHARE
TWEET

Untitled

a guest May 23rd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 5
  4.  
  5. typedef struct BUFFER {
  6.     int array[MAX];
  7.     int f, r;
  8. } BUFFER;
  9.  
  10. typedef struct node {
  11.     int num;
  12.     struct node *next;
  13. } NODE;
  14.  
  15. int isFull(BUFFER *buffer);
  16. int isEmpty(BUFFER *buffer);
  17.  
  18. int addToBuffer(BUFFER *buffer, int data);
  19. void options(BUFFER *buffer, NODE **tos);
  20. int delete(BUFFER *buffer, int *data);
  21. int writeFromBuffer(BUFFER *buffer);
  22.  
  23. void addToStack(NODE **tos, int *num);
  24. int pop(NODE **tos, int *num);
  25. void writeFromStack(NODE **tos);
  26.  
  27. int main() {
  28.     BUFFER buffer; buffer.f = buffer.r = -1; char c; int num;
  29.     NODE *tos;
  30.  
  31.     options(&buffer, &tos);
  32.  
  33.     writeFromStack(&tos);
  34.     writeFromBuffer(&buffer);
  35.     return 0;
  36. }
  37.  
  38. void options(BUFFER *buffer, NODE **tos){
  39.     int o, pom;
  40.     printf("Izaberi operaciju: \nDodavanje novog elementa u bafer [1] \nUklanjanje elementa iz bafera    [2] \nIspis svih elemenata iz bafera   [0]\n");
  41.     scanf("%d", &o);
  42.  
  43.     if(o == 1){
  44.  
  45.         int n;
  46.         printf("Unesite prirodan num\n");
  47.         scanf("%d", &n);
  48.  
  49.         if(addToBuffer(buffer, n) == 0)
  50.             printf("Bafer je pun.\n");
  51.         else
  52.             addToStack(tos, &n);
  53.        
  54.         options(buffer, tos);
  55.  
  56.     } else if (o == 2) {
  57.  
  58.         int n;
  59.         delete(buffer, &n);
  60.         options(buffer, tos);
  61.  
  62.     }else if (o == 0) {
  63.         return;
  64.     }else
  65.         printf("Unesena opcija ne postoji.");
  66.        
  67. }
  68.  
  69.  
  70. //stack
  71. void addToStack(NODE **tos, int *num) {
  72.   NODE *new = (NODE *)malloc(sizeof(NODE));
  73.   new->num = *num;
  74.   new->next = *tos;
  75.   *tos = new;
  76. }
  77.  
  78. int pop(NODE **tos, int *num) {
  79.   if (*tos == 0) return 0;
  80.   NODE *p = *tos;
  81.   *num = p->num;
  82.   *tos = p->next;
  83.   free(p);
  84.   return 1;
  85. }
  86.  
  87. void writeFromStack(NODE **tos) {
  88.   int num; int rb = 1;
  89.  
  90.   while (pop(tos, &num))
  91.     printf("%d. broj: %d\n", rb++, num);
  92.  
  93. }
  94.  
  95. //buffer
  96. int addToBuffer(BUFFER *buffer, int data){
  97.   if (isFull(buffer))
  98.     return 0;
  99.   if (buffer->f == -1)
  100.     buffer->f = 0;
  101.   buffer->r = (buffer->r + 1) % MAX;
  102.   buffer->array[buffer->r] = data;
  103.   return 1;
  104. }
  105.  
  106. int delete(BUFFER *buffer, int *data){
  107.   if (isEmpty(buffer))
  108.     return 0;
  109.  
  110.   *data = buffer->array[buffer->f];
  111.  
  112.   if (buffer->f == buffer->r)
  113.     buffer->f = buffer->r = -1;
  114.   else
  115.     buffer->f = (buffer->f + 1) % MAX;
  116.  
  117.   return 1;
  118. }
  119.  
  120. int writeFromBuffer(BUFFER *buffer) {
  121.   if (isEmpty(buffer))
  122.     return 0;
  123.  
  124.   int i = buffer->f;
  125.  
  126.   if (buffer->f <= buffer->r){
  127.  
  128.     while (i <= buffer->r)
  129.       printf("%d ", buffer->array[i++]);
  130.  
  131.   } else {
  132.    
  133.     while (i <= MAX - 1)
  134.       printf("%d ", buffer->array[i++]);
  135.    
  136.     i = 0;
  137.     while (i <= buffer->r)
  138.       printf("%d ", buffer->array[i++]);
  139.  
  140.   }
  141.  
  142.   return 1;
  143. }
  144.  
  145. int isFull(BUFFER *buffer) {
  146.     return (buffer->r + 1) % MAX == buffer->f;
  147. }
  148.  
  149. int isEmpty(BUFFER *buffer) {
  150.     return buffer->f == -1;
  151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top