Stack PUSH & POP Implementation Using Arrays
Stack PUSH & POP Implementation Using Arrays
Arrays
/*STACK PUSH() AND POP() IMPLEMENTATION USING ARRAYS*/
#include <stdio.h>
#include<conio.h>
#define MAX 5
int top, status;
/*PUSH FUNCTION*/
void push (int stack[], int item)
{
if (top == (MAX-1))
status = 0;
else
{
status = 1;
++top;
stack [top] = item;
}
}
/*POP FUNCTION*/
int pop (int stack[])
{
int ret;
if (top == -1)
{
ret = 0;
status = 0;
}
else
{
status = 1;
ret = stack [top];
--top;
}
return ret;
}
/*FUNCTION TO DISPLAY STACK*/
void display (int stack[])
{
int i;
printf ("\nThe Stack is: ");
if (top == -1)
printf ("empty");
else
if (status)
{
printf ("\nThe Popped item is %d.
After Popping: ");
display (stack);
}
else
printf ("\nStack underflow on Pop");
break;
default:
printf ("\nEND OF EXECUTION");
}
}while (ch != 3);
getch();
}