Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define max 10
- struct stack
- {
- int count;
- int top;
- int items[max];
- };
- void main()
- {
- int i,j,b=1,a,pp,n;
- struct stack p1;
- p1.top=-1;
- p1.count=1;
- printf("enter choice\n1 to push\n2 to pop\n3 to display\n");
- while(b!=0)
- {
- scanf("%d",&i);
- switch(i)
- {
- case 1:
- printf("enter the number of elements to push \n");
- scanf("%d",&n);
- printf("enter the element to push\n");
- for(j=1;j<=n;j++)
- {
- scanf("%d",&a);
- if(p1.top==max)
- {
- printf("stack is full\n only %d elements can be pushed\n",&j);
- }
- else
- {
- p1.top++;
- p1.items[p1.top]=a;
- p1.count++;
- }
- }
- break;
- case 2:
- if(p1.top==-1)
- {
- printf("stack is empty\n");
- }
- else
- {
- pp=p1.items[p1.top];
- p1.top--;
- p1.count--;
- printf("the popped element is %d\n",pp);
- }
- break;
- case 3:
- printf("the elements of a stack are:\n");
- for(j=1;j<(p1.count);j++)
- printf("%d\n",(p1.items[j-1]));
- break;
- default:
- printf("invalid operator\n");
- break;
- }
- printf("enter 1 to continue or 0 to exit\n");
- scanf("%d",&b);
- if(b==1)
- printf("enter choice\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment