Advertisement
Guest User

FIFO

a guest
Mar 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. #define STACK_SIZE 100
  6. int *adressen[STACK_SIZE];
  7.  
  8. //Store value at end of stack
  9. int push(int value) {
  10.     if (endofstack() <= STACK_SIZE)
  11.     {
  12.         adressen[endofstack()] = malloc(sizeof(int));
  13.         *adressen[endofstack() - 1] = value;
  14.         return 1;
  15.     }
  16.     else
  17.     {
  18.         return 0;
  19.     }
  20. }
  21.  
  22. //return the first value of the stack and shift all to the left
  23. int pop() {
  24.     int value = *adressen[0];
  25.  
  26.     for (int i = 0; i < STACK_SIZE; i++)
  27.     {
  28.         adressen[i] = adressen[i + 1];
  29.     }
  30.     adressen[STACK_SIZE] = NULL;
  31.     return value;
  32. }
  33.  
  34. //return the position of the end of the stack (empty adresses filled with NULL)
  35. int endofstack() {
  36.     int i = 0;
  37.     while (adressen[i] != NULL) {
  38.         i++;
  39.     }
  40.     return i;
  41. }
  42.  
  43. int main() {
  44.     for (int i = 0; i < STACK_SIZE; i++)
  45.     {
  46.         adressen[i] = NULL;
  47.     }
  48.     push(123);
  49.     printf("%d\n", pop());
  50.  
  51.     for (int i = 0; endofstack() < STACK_SIZE; i++)
  52.     {
  53.         push(i);
  54.     }
  55.  
  56.     while (endofstack() > 0) {
  57.         printf("%d\n", pop());
  58.     }
  59.  
  60.     system("PAUSE");
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement