C program for push, pop and peep operations in a stack using array.

stack.c

C program to implement push, pop and peep operations in a stack using an array is as follows:

#include<stdio.h>
#include<conio.h>

#define SIZE 5
int top = -1;
int stack[SIZE];

//Function prototypes
void push(int item);
int pop();
int peep();

void main()
{
	int item, choice, cont = 1;
	clrscr();

	while(cont == 1)
	{
		printf("\n1.Push onto stack.\n");
		printf("\n2.Pop from stack.\n");
		printf("\n3.Peep into stack.\n");

		printf("\nEnter your choice: ");
		scanf("%d",&choice);

		switch(choice)
		{
			case 1:
				printf("\nEnter the value of item: ");
				scanf("%d",&item);
				push(item);
				break;

			case 2:
				item = pop();
				if(item != NULL)
				{
					printf("\nItem popped out: %d\n",item);
				}				
				break;

			case 3:
				item = peep();
				if(item != NULL)
				{
					printf("\nItem at top of stack: %d\n",item);
				}				
				break;

			default:
				printf("\nInvalid choice.\n");
				break;
		}

		printf("\nDo you want to continue (1/0): ");
		scanf("%d",&cont);
	}

	getch();
}

//Function for push operation
void push(int item)
{
	if(top >= SIZE-1)
	{
		printf("\nStack Overflow. Push not possible.\n");
	}
	else
	{
		top = top+1;
		stack[top] = item;
	}
}

//Function for pop operation
int pop()
{
	int item = NULL;

	if(top <= -1)
	{
		printf("\nStack Underflow. Pop not possible.\n");
	}
	else
	{
		item = stack[top];
		stack[top] = NULL;
		top = top-1;
	}
	return(item);
}

//Function for peep operation
int peep()
{
	int item = NULL;

	if(top <= -1)
	{
		printf("\nStack Underflow. No element in stack.\n");
	}
	else
	{
		item = stack[top];
	}
	return(item);
}