Push operation in Stack

http://install4install.com

Push Operation

In this operation,the element is inserted at the top of stack(Fig 4).In order to insert element we need to check whether TOP = MAX-1.If  yes,then insertion  is not possible.If  element is still  tried for insertion , OVERFLOW message will be printed on screen as the stack do not have extra space to handle new element.If space is present and element is inserted,then the value of top will be incremented by 1 .

For example

Screen Shot 2014-04-08 at 5.01.32 PM

Fig 5 : Push Operation in stack

Program 1

#include<stdio.h>
int main()
{
	int stack[10];
	int max;
	int num;
	int top = -1;
	int i;
	int item;
	//printf("Enter the maximum size of stack");
	scanf("%d",&max);
	//printf("Enter the no of elements in stack");
	scanf("%d",&num);
        //printf("Enter the elements of stack");
	for(i=0;i<num;i++)//Fig 7
	{
		scanf("%d\n",&stack[i]);
		top++;
	}

	if(top == max -1)//Fig 8
	{
		printf("Overflow");
	}
	else
	{
		//printf("Enter the item you want to insert\n");
		scanf("%d",&item);
		top = top + 1;//Fig 6
		stack[top] = item;
		printf("The stack after push operation is \n");
		for(i=0;i<=num;i++)
	{
	 printf("%d\n",stack[i]);
		top++;
	}
	}

	return 0;
}

Run

 Illustration of Program 1

Screen Shot 2014-04-09 at 1.11.26 PM

Fig 6 : Push Operation

 

Screen Shot 2014-04-09 at 1.03.37 PM

Fig 7 : Working of for loop

Screen Shot 2014-04-09 at 1.03.57 PM

Fig 8 : Working of if statement

 

 



Short URL: http://tinyit.cc/8c08e2
Author: Cusp2207 on July 18, 2014
Category: Algorithms, Computer Science, Data Structures
Tags:

Leave a Reply

Last articles