Week 6 programs to implement stack and its operations.

Source Code

#include<stdio.h>
#include<stdlib.h>
#define Size 4
int Top=-1, inp_array[Size];
void Push();
void Pop();
void show();
int main()
{
	int choice;
	while(1)
	{
		printf(“\nOperations performed by Stack”);
		printf(“\n1.Push the element\n2.Pop the element\n3.Show\n4.End”);
		printf(“\n\nEnter the choice:”);
		Scanf(“%d”,&choice);
		Switch(choice)
		{
			case 1: Push();
					Break;
			case 2: Pop();
					Break;
			case 3: show();
					Break;
			case 4: exit(0);

			Default: printf(“\nInvalid choice!!”);
		}
	}
}
void Push()
{
	int x;
	if(Top==Size-1)
	{
		printf(“\nOverflow!!”);
	}
	else
	{
		printf(“\nEnter element to be inserted to the stack:”);
		Scanf(“%d”,&x);
		Top=Top+1;
		Inp_array[Top]=x;
	}
}
void Pop()
{
	if(Top==-1)
	{
		printf(“\nUnderflow!!”);
	}
	else
	{
		printf(“\nPopped element:  %d”,inp_array[Top]);
		Top=Top-1;
	}
}
void show()
{
	if(Top==-1)
	{
		printf(“\nUnderflow!!”);
	}
	else
	{
		printf(“\nElements present in the stack: \n”);
		For(int i=Top;i>=0;--i)
			printf(“%d\n”,inp_array[i]);
	}
}