Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_NO_SECURE_WARNINGS
- #define _CRT_SECURE_NO_DEPRECATE
- #include<stdio.h>
- #define MAX 5
- typedef struct
- {
- int tos;
- int s[MAX];
- }stack;
- void push(stack *t, int ele)
- {
- if (t->tos == MAX - 1)
- {
- printf("\nStack Overflow\n");
- return;
- }
- else
- {
- t->tos++;
- t->s[t->tos] = ele;
- }
- }
- void display(stack *t)
- {
- int i;
- if (t->tos == -1)
- {
- printf("\nStack Empty\n");
- return;
- }
- else
- {
- for (i = t->tos; i >= 0; i--)
- printf("\t%d",t->s[i]);
- printf("\n");
- }
- }
- int pop(stack *t)
- {
- int x;
- if (t->tos == -1)
- {
- printf("\nStack Underflow\n");
- return -1;
- }
- else
- {
- x = stacktop(t);
- t->tos--;
- return x;
- }
- }
- int stacktop(stack *t)
- {
- return t->s[t->tos];
- }
- void main()
- {
- stack s;
- s.tos = -1;
- int ch, ele;
- while (1)
- {
- printf("\nEnter your choice\n1.Push\n2.Pop\n3.Display\n4.Stack Top\n5.Exit\n\n");
- scanf("%d", &ch);
- if (ch == 5)
- break;
- switch (ch)
- {
- case 1:
- if (s.tos == MAX - 1)
- printf("\nStack Overflow\n");
- else
- {
- printf("\nEnter the element to be pushed\n");
- scanf("%d", &ele);
- push(&s, ele);
- display(&s);
- }
- break;
- case 2:
- if (s.tos == -1)
- printf("\nStack Underflow\n");
- else
- printf("\nThe popped element is %d", pop(&s));
- break;
- case 3:
- display(&s);
- break;
- case 4:
- if (s.tos == -1)
- printf("\nStack Empty");
- else
- printf("\nElement at the top of the stack is %d", stacktop(&s));
- break;
- default:
- printf("\nInvalid Choice.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement