Tuesday 13 May 2014

Array Implementation of a Stack

Created By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
==================================
Array Implementation of a Stack

........................................................
#include 
#define MAX 10/*j ava2 s. c o m*/
#include

void push(int stack[], int *top, int value){
if(*top < MAX ){
*top = *top + 1;
stack[*top] = value;
}else{
printf("The 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("The stack is empty can not pop a value\n");
exit(0);
}
}

void main()
{
int stack[MAX];
int top = -1;
int n,value;

push(stack,&top,1);
push(stack,&top,2);
push(stack,&top,3);

pop(stack,&top,&value);
printf("The value poped is %d\n",value);

pop(stack,&top,&value);
printf("The value poped is %d\n",value);
}

0 comments:

Post a Comment