Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <stdlib.h>
- #define STACKSIZE 5 // the size of our stack
- int myStack[STACKSIZE]; // the stack
- int stackTop=0; // current top of stack - next item goes here
- void push(int data)
- {
- if (stackTop==STACKSIZE)
- {
- printf("Stack overflow\n");
- exit(1);
- }
- myStack[stackTop]=data;
- stackTop++;
- }
- int pop()
- {
- if (stackTop==0)
- {
- printf("Stack underflow\n");
- exit(2);
- }
- stackTop--;
- return myStack[stackTop];
- }
- int main()
- {
- push(1);
- push(2);
- printf("%d\n", pop()); // 2
- printf("%d\n", pop()); // 1
- push(1); push(1); push(1); push(1); push(1); push(1); // stack overflow
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment