Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define Size 5
- void push(int);
- int pop (void);
- void traverse(void);
- int isFull(void);
- int isEmpty(void);
- void traverse(void);
- int stack[Size];
- int top=-1;
- void main()
- { int ch,item;
- while(1)
- {
- printf("1.Push\n");
- printf("2.Pop\n");
- printf("3.Peek\n");
- printf("4.Traverse\n");
- printf("5.Quit\n");
- printf("Enter Your Choice: ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:printf("Enter Element:");
- scanf("%d",&item);
- push(item);
- break;
- case 2:item=pop();
- if(item==0)
- {
- printf("stack is underflow\n");
- }
- else
- {
- printf("Popped item: %d\n",item);
- }
- break;
- case 3:peek();
- break;
- case 4:traverse();
- break;
- case 5:exit(0);
- break;
- default:printf("Invalid Imput\n");
- }
- }
- }
- void push(int ele)
- {
- if(isFull())
- {
- printf("Stack is overflow\n");
- }
- else
- {
- top++;
- stack[top]=ele;
- printf("%d Pushed\n",ele);
- }
- }
- int isFull()
- {
- if(top==Size-1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int pop ()
- {
- if(isEmpty())
- {
- return 0;
- }
- else{
- return stack[top--];
- }
- }
- int isEmpty()
- {
- if(top==-1)
- {
- return 1;
- }
- else{
- return 0;
- }
- }
- void peek()
- {
- if(isEmpty())
- {
- printf("stack is empty\n");
- }
- else
- {
- printf("peek element :%d\n",stack[top]);
- }
- }
- void traverse()
- {
- if(isEmpty())
- {
- }
- else
- {
- int i;
- printf("stack elements : \n");
- for(i=0;i<=top;i++)
- {
- printf("%d\n",stack[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement