Home » Blog » Stack Push And Pop Operation

Stack Push And Pop Operation

Here, We are performing push and pop operation of stack.

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

void push();
void pop();
void main()
{
	int ch,c;
	clrscr();
	do
	{
	printf("\n***SELECT OPERATION OF STACK**\n");
	printf("\n1.PUSH OPERATION\n2.POP OPERATION\n");
	printf("\nEnter you choice::");
	scanf("%d",&ch);
	switch(ch)
	{
		case 1:
			push();
			break;
		case 2:
			pop();
			break;
		default:
			printf("\n**INVALID CHOICE**\n");
	}
	printf("\nDOU YOU WANT TO QUIT(0 or 1)::");
	scanf("%d",&c);
	}while(c==0);
}
void push()
{
	int a[10],n,i,top=-1,no;
	printf("\n***PUSH OPERATION**\n");
	printf("\nEnter the size of array::");
	scanf("%d",&n);
	printf("\n**Enter array elements**\n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		top++;
	}
	printf("\nEnter element to insert::");
	scanf("%d",&no);
	if(top>=n)
	{
		printf("\n**STACK IS OVERFLOW**\n");
	}
	else
	{
		top++;
		a[top]=no;
	}
	printf("\n***AFTER POUSH OPERATION**\n");
	for(i=0;i<=top;i++)
		printf("%d\n",a[i]);
}

void pop()
{
	int a[10],n,i,top=-1,no=0;
	printf("\n***POP OPERATION***\n");
	printf("\nEnter the size of array::");
	scanf("%d",&n);
	printf("\n**Enter elements**\n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		top++;
	}
	if(top==0)
		printf("\n***STACK IS UNDERFLOW***\n");
	else
	{
		no=a[top];
		top--;
	}
	printf("\n***AFTER POP OPERATION***\n");
	for(i=0;i<=top;i++)
		printf("%d\n",a[i]);
}

 

Leave a Reply

Your email address will not be published.