Facebook
From Buff Lizard, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 175
  1. #include <stdio.h>
  2.  
  3. #define SIZE 10
  4.  
  5. static int top = -1;
  6. int stack[SIZE];
  7.  
  8. int isEmpty()
  9. {
  10.   if (-1 == top) return 1;
  11.   else return 0;
  12. }
  13.  
  14. int isFull()
  15. {
  16.   if (SIZE == top) return 1;
  17.   else return 0;
  18. }
  19.  
  20. void push(int p_variable)
  21. {
  22.   if (!isFull())
  23.   {
  24.     top = top + 1;
  25.     stack[top] = p_variable;
  26.   }
  27.   else
  28.   {
  29.     printf("Stack is full.\n");
  30.   }
  31. }
  32.  
  33. int pop()
  34. {
  35.   int tmp;
  36.  
  37.   if (!isEmpty())
  38.   {
  39.     tmp = stack[top];
  40.     top = top - 1;
  41.     return tmp;
  42.   }
  43.   else
  44.   {
  45.     printf("Stack is empty.\n");
  46.   }
  47. }
  48.  
  49. void main()
  50. {
  51. prin
  52.   push(3);
  53.   push(2);
  54.   push(7);
  55.   push(1);
  56.   push(10);
  57.  
  58.   printf("Stack has items:\n");
  59.  
  60.     while (!isEmpty())
  61.     {
  62.       int tmp = pop();
  63.       printf("%d\n", tmp);
  64.     }
  65. }
  66.