Push and Pop operation of stack.


 

Push and Pop operation of stack.

This tutorial demonstrate the push and pop operation of stack using array.

This tutorial demonstrate the push and pop operation of stack using array.

Description:

In this tutorial of data-structure you will see push and pop operation of stack. In the previous tutorial is clearly explained the push pop operation. The drawback of implementing stack is that the size of stack is fixed it cannot grow or shrink.

Code:

#include <stdio.h>
#include <stdlib.h>
void push(int stack[], int *top, int value)
{
	if(*top < 4 )
 	{
	*top = *top + 1;
	stack[*top] = value;
	printf("\nStack element should be less than four\n");
	}
	else
	{
	printf("\nThe stack is full can not push a  value\n");
	exit(0);
	}
}
void pop(int stack[], int *top, int * value)
{
	if(*top >= 0 )
	{
		*value = stack[*top];
		*top = *top - 1;
	}
	else
	{
	printf("\nThe stack is empty can not pop a  value\n");
	exit(0);
	}
}
	void main()
	{
	int stack[4];
	int top = 0;
	int n,value;
	do
	{
	  do
	  {
	 printf("\nEnter the element to be pushed\n");
	 scanf("%d",&value);
	 push(stack,&top,value);
	 printf("\nEnter 1 to continue and other key to pop\n");
	 scanf("%d",&n);
	 printf("");
	  } 	  while(n == 1);
		  printf("\nEnter 1 to pop an element\n");
		  scanf("%d",&n);
		  while( n == 1)
		 {
		 pop(stack,&top,&value);
		 printf("\nThe value poped is %d",value);
		 printf("\nEnter 1 to pop an element\n");
		 scanf("%d",&n);
		 }
	  printf("\nEnter 1 to continue and other key to push\n");
	  scanf("%d",&n);
	} while(n == 1);
}

Output:

Download this code

Ads