Push and Pop operation of stack - DS TUTORIAL - onwnxcvnne
Headlines News :
Home » , , » Push and Pop operation of stack - DS TUTORIAL

Push and Pop operation of stack - DS TUTORIAL

Written By jomon on Monday 16 April 2012 | 09:16

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:

Share this article :

0 comments:

Speak up your mind

Tell us what you're thinking... !

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. onwnxcvnne - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger