Home Tutorial Datastructure Push and Pop operation of stack.

 
 

Share on Google+Share on Google+
Push and Pop operation of stack.
Posted on: June 18, 2010 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for Push and Pop operation of stack.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 18, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com