/* Implementation of Stack using array
S.Y.CSE_DS_2021-22 */
# include <stdio.h>
# include <conio.h>
# include <process.h>
# define MAX_SIZE 10
# define bottom -1
void initStack();
int isOverflow();
int isUnderflow();
void push(int);
void pop();
void peep();
void traverse();
int stack[MAX_SIZE];
int top;
void main()
{
int choice, item;
clrscr();
do
{
printf("\n....MENU.....\n");
printf("1. Initialize stack\n");
printf("2. Push\n");
printf("3. Pop\n");
printf("4. Peep\n");
printf("5. Traverse\n");
printf("6. Quit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
if (choice <1 || choice>6)
{
printf("Invalid choice...try again\n");
continue;
}
switch(choice)
{
case 1: initStack();
break;
case 2: printf("Enter the item to Push\n");
scanf("%d",&item);
push(item);
break;
case 3: pop();
break;
case 4: peep();
break;
case 5: traverse();
break;
case 6: printf("End of program\n");
exit(0);
}
} while (choice != 6);
}
void initStack()
{
top=bottom;
printf("Stack initialized\n");
}
int isOverflow()
{
if (top==MAX_SIZE-1)
return(1);
else
return(0);
}
int isUnderflow()
{
if (top==bottom)
return(1);
else
return(0);
}
void push(int newItem)
{
if (isOverflow())
printf("Stack overflow\n");
else
{
top = top +1;
stack[top] = newItem;
}
}
void pop()
{
if (isUnderflow())
printf("Stack underflow\n");
else
{
printf("Item Popped = %d\n", stack[top]);
top = top-1;
}
}
void peep()
{
if (isUnderflow())
printf("Stack is empty\n");
else
printf("Top of stack = %d\n", stack[top]);
}
void traverse()
{
int i;
if (isUnderflow())
printf("Stack is empty\n");
else
{
printf("Stack contents are :\n");
for(i=top; i>bottom; i--)
printf(" %d\n", stack[i]);
}
}