/*Write a C program to demonstrate the working of a stack of size N using an array. 
The elements of the stack may be assumed to be of type integer or real. The operations 
to be supported are: 1.PUSH 2.POP 3.DISPLAY. The program should print appropriate 
messages for stack overflow, stack underflow and stack empty.
c4sstack.c*/


#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int top,stack[MAX];

// Initializing the TOP pointer
void init()
{
	top=-1;
}

// Push an item into the TOP of the stack
void push(int x)
{
	stack[++top]=x;
}

// Pop the TOP element from the stack
int pop()
{
	return(stack[top--]);
}

// Display the contents of the stack
void display()
{
	int i;
	printf("The stack content is:\n");
	for(i=top;i>=0;i--)
		printf("%d\n",stack[i]);
}

// Check for the Stack Overflow condition
int overflow()
{
	if(top>=MAX-1)
		return 1;
	else
		return 0;
}

// Check for the Stack Underflow condition
int underflow()
{
	if(top<=-1)
		return 1;
	else
		return 0;
}

// Check for the Stack Empty condition
int empty()
{
	if(top==-1)
		return 1;
	else
		return 0;
}

// Interactive Menu Driven program to implement Stack concept.
main()
{
	int item,choice;
	init();
	while(1)
	{
		printf("\nMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
		printf("Enter your choice\n");
		scanf("%d",&choice);
		switch(choice)
		{
			case 1: // PUSH Operation
					if(overflow())
						printf("stack overflow\n");
					else
					{
						printf("Enter an element to be inserted\n");
						scanf("%d",&item);
						push(item);
					}
					break;

			case 2: // POP Operation
					if(underflow())
						printf("stack underflow\n");
					else
						printf("The deleted item is %d\n",pop());
					break;

			case 3: // DISPLAY Operation
					if(empty())
						printf("stack is empty\n");
					else
						display();
					break;

			case 4: // Quit the program
					exit(0);

			default: // Invalid Selection
					printf("Invalid choice\n");
		}
	}
} // End of main