Advertisement
rafikamal

Stack

Feb 10th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE 100
  5.  
  6. int stack[SIZE];
  7. int top = 0;
  8.  
  9. void push(int value);
  10. int pop();
  11.  
  12. int main()
  13. {
  14.     push(10);
  15.     push(18);
  16.     printf("%d\n", pop());
  17.     push(12);
  18.     push(4);
  19.     printf("%d\n", pop());
  20.     printf("%d\n", pop());
  21.     printf("%d\n", pop());
  22.    
  23.     return 0;
  24. }
  25.  
  26. void push(int value)
  27. {
  28.     if(top == SIZE)
  29.     {
  30.         printf("Stack Full\n");
  31.         exit(1);
  32.     }
  33.     stack[top++] = value;
  34. }
  35.  
  36. int pop()
  37. {
  38.     if(top == 0)
  39.     {
  40.         printf("Stack Empty\n");
  41.         exit(2);
  42.     }
  43.     return stack[--top];
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement