Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #define max 50
- void push();
- void pop();
- void display();
- int menu();
- int stack[max],top=0;
- int main()
- {
- intch;
- do
- {
- {
- ch=menu();
- switch(ch)
- case 1: push();
- break;
- case 2: pop();
- break;
- case 3: display();
- break;
- case 4: printf("----Stack exited----");
- break;
- default: printf("Enter a valid choice!");
- }
- }
- while(ch!=4);
- return 0;
- }
- int menu()
- {
- int ch;
- printf("-----Select the operation you want to perform on the stack-----\n");
- printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
- printf("Enter your choice\t");
- scanf("%d",&ch);
- return ch;
- }
- void push()
- {
- if(top==max) printf("Stack overflow!\n");
- else
- {
- int element;
- printf("----Enter element----\n");
- scanf("%d",&element);
- printf("----Element has been pushed----\n");
- stack[top++]=element;
- }
- }
- void pop()
- {
- if(top==0) printf("----Stack Underflow!----\n");
- else
- {
- top--;
- printf("----Element has been popped out----\n");
- }
- }
- void display()
- {
- if(top==0) printf("----Stack is empty!----\n");
- else
- {
- {
- inti;
- printf("----Displaying stack elements----\n");
- for(i=0;i<top;i++)
- printf("%d\n",stack[i] );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement