C program for Stack implementation using array

#include "stdio.h"
#include "conio.h"

#define size 5

int stack[size] = {0};

int top = -1;

void push(int value)
{
if(top < size)
{
top++;
stack[top] = value;
}
else
{
printf("Stack overflow n");
}
}

void pop()
{
if(top >= 0)
{
printf("The popped element is:t %d n", stack[top]);
stack[top] = 0;
top--;
}

else
{
printf("Stack underflow n");
}

}
void display()
{

int i;

for(i=size - 1;i>=0;i--)
{
printf(" %d n",stack[i]);
}
}

void tos()
{
printf("The value at top is %d", stack[top]);
}

int main()
{

int i,j, ch=0 ,value = 0;

while(1)
{
printf("n Please Enter the choice. n");
printf("1 for pushn 2 for pop n 3 for displayn 4 for top t  n 5 for exit");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("n Please Enter the number to be inserted: t");
scanf("%d", &value);
push(value);
break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:
tos();

break;

case 5:

exit(0);

}
}

}

Leave a Reply