Static Implementation Of Stack
CODE
π
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 20
- typedef struct
- {
- int data[MAX];
- int top;
- }STACK;
- void initstack(STACK *ps)
- {
- ps->top = -1;
- }
- void push(STACK *ps, int num)
- {
- ps->top++;
- ps->data[ps->top] = num;
- }
- int pop(STACK *ps)
- {
- return(ps->data[ps->top--]);
- }
- int isempty(STACK *ps)
- {
- return(ps->top == -1);
- }
- int isfull(STACK *ps)
- {
- return(ps->top==MAX-1);
- }
- void main()
- {
- int n, choice;
- STACK s1;
- initstack(&s1);
- do
- {
- printf("\n1.PUSH \n2.POP \n3.EXIT");
- printf("\n Enter Your Choice:");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- if(isfull (&s1))
- printf("\n Stack Overflow");
- else
- {
- printf("Enter the element to be pushed:");
- scanf("%d", &n);
- push(&s1, n);
- }
- break;
- case 2:
- if(isempty(&s1)==1)
- printf("\n Stack Underflow");
- else
- printf("The popped element is: %d\n", pop(&s1));
- break;
- }
- }
- while(choice != 3);
- }
πExecuteπ
//Output
/*
1.PUSH
2.POP
3.EXIT
Enter Your Choice:1
Enter the element to be pushed:5
1.PUSH
2.POP
3.EXIT
Enter Your Choice:2
The popped element is: 5
1.PUSH
2.POP
3.EXIT
Enter Your Choice:3
*/
//ThE ProFessoR
Comments
Post a Comment