Advertisement
yuawn

stack.c

Oct 19th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX 500
  4.  
  5. struct stack{
  6.     int stk[MAX] , t;
  7.     void init(){ t = 0; }
  8.     bool isempty(){ return !t; }
  9.     bool isfull(){ return t == MAX - 1; }
  10.     void push( int n ){
  11.         if( t == MAX ) puts("Full!");
  12.         else stk[t++] = n;
  13.     }
  14.     int pop(){
  15.         if( !t ) puts("Empty!");
  16.         return stk[--t];
  17.     }
  18. };
  19.  
  20. int main(){
  21.     stack a;
  22.     a.init();
  23.     puts( a.isempty() ? "Empty stack!" : "Not empty" );
  24.     a.push(1);
  25.     a.push(2);
  26.     a.push(3);
  27.     a.push(4);
  28.     a.pop();
  29.     a.pop();
  30.     a.push(100);
  31.     printf( "%d\n" , a.pop() );
  32.     printf( "%d\n" , a.pop() );
  33.     printf( "%d\n" , a.pop() );
  34.     printf( "%d\n" , a.pop() );
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement