Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 100
- int S[SIZE]; // stack declaration
- int top = -1 ; // initially stack is empty
- void push(int x){
- if(top == (SIZE - 1) )
- {
- printf("\nStack overflow.....\n");
- return;
- }
- S[++top] = x;
- }
- void pop(){
- if(top == -1)
- {
- printf("\nStack underflow.....\n");
- return;
- }
- top--;
- }
- int topElement(){
- return S[top];
- }
- int isEmpty(){
- if(top == -1)
- return 1;
- else
- return 0;
- }
- void print(){
- int i;
- printf("\nthe stack is : \n");
- for(i=0;i<=top;i++){
- printf("%d\t",S[i]);
- }
- }
- int main(){
- push(2);print();
- push(10);print();
- pop();print();
- push(7);print();
- push(5);print();
- printf("\nthe top element is : %d\n",topElement());
- if(isEmpty() == 1) printf("\nthe Stack is empty\n");
- else printf("\nthe Stack is not empty\n");
- pop();print();
- pop();print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement