SHARE
TWEET

Untitled

a guest May 23rd, 2019 69 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.     if(o == 1){
  43.         int n;
  44.         printf("Unesite prirodan num\n");
  45.         scanf("%d", &n);
  46.         if(addToBuffer(buffer, n) == 0)
  47.             printf("Bafer je pun.\n");
  48.         else
  49.             addToStack(tos, &n);
  50.        
  51.         options(buffer, tos);
  52.     }else if (o == 2) {
  53.         int n;
  54.         delete(buffer, &n);
  55.         options(buffer, tos);
  56.     }else if (o == 0) {
  57.         return;
  58.     }else
  59.         printf("Unesena opcija ne postoji.");
  60.        
  61. }
  62.  
  63.  
  64. //stack
  65. void addToStack(NODE **tos, int *num) {
  66.   NODE *new = (NODE *)malloc(sizeof(NODE));
  67.   new->num = *num;
  68.   new->next = *tos;
  69.   *tos = new;
  70. }
  71.  
  72. int pop(NODE **tos, int *num) {
  73.   if (*tos == 0) return 0;
  74.   NODE *p = *tos;
  75.   *num = p->num;
  76.   *tos = p->next;
  77.   free(p);
  78.   return 1;
  79. }
  80.  
  81. void writeFromStack(NODE **tos) {
  82.   int num; int rb = 1;
  83.  
  84.   while (pop(tos, &num))
  85.     printf("%d. broj: %d\n", rb++, num);
  86.  
  87. }
  88.  
  89. //buffer
  90. int addToBuffer(BUFFER *buffer, int data){
  91.   if (isFull(buffer))
  92.     return 0;
  93.   if (buffer->f == -1)
  94.     buffer->f = 0;
  95.   buffer->r = (buffer->r + 1) % MAX;
  96.   buffer->array[buffer->r] = data;
  97.   return 1;
  98. }
  99.  
  100. int delete(BUFFER *buffer, int *data){
  101.   if (isEmpty(buffer))
  102.     return 0;
  103.  
  104.   *data = buffer->array[buffer->f];
  105.  
  106.   if (buffer->f == buffer->r)
  107.     buffer->f = buffer->r = -1;
  108.   else
  109.     buffer->f = (buffer->f + 1) % MAX;
  110.  
  111.   return 1;
  112. }
  113.  
  114. int writeFromBuffer(BUFFER *buffer) {
  115.   if (isEmpty(buffer))
  116.     return 0;
  117.   int i = buffer->f;
  118.   if (buffer->f <= buffer->r)
  119.   {
  120.     while (i <= buffer->r)
  121.       printf("%d ", buffer->array[i++]);
  122.   }
  123.   else
  124.   {
  125.     while (i <= MAX - 1)
  126.       printf("%d ", buffer->array[i++]);
  127.     i = 0;
  128.     while (i <= buffer->r)
  129.       printf("%d ", buffer->array[i++]);
  130.   }
  131.   return 1;
  132. }
  133.  
  134. int isFull(BUFFER *buffer) {
  135.     return (buffer->r + 1) % MAX == buffer->f;
  136. }
  137.  
  138. int isEmpty(BUFFER *buffer) {
  139.     return buffer->f == -1;
  140. }
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