Stack 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>
// Maximum size of stack is 5(that i have taken)
#define max 5

// stack[] and top is declared and initialised


int stack[max], top = -1;
// This function is used to POP/delete item from stack
void pop()
{
int item;
if (top == -1)
{
printf("Empty Stack: Underflow condition occured");
return;
}
item = stack[top];
printf("%d is POPPED\n",item);
top--;
return;
}
// This function is used to PUSH item onto the STACK
void push(int item)
{
if (top == max - 1)
{
printf("Overflow Condition occured");
return;
}
top++;
stack[top] = item;
return;
}
// This function is used to display the ITEMS stored in STACK
void show()
{
int item, i;
if (top == -1)
{
printf("Empty Stack\n");
return;
}
for (i = 0; i <= top; i++)
{
printf("%d ->", stack[i]);
}

printf("\n");

return;
}

int main()
{
int item, ch;
// This is a menu driven Program
while (1)
{
printf("1. PUSH\n2.POP\n3.Show\n4.Quit\n");
printf("Enter your choice(1,2,3 or 4) ... ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter an item to be pushed ");
scanf("%d", &item);
push(item);
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
break;
default:
printf("Invalid Input");
break;
}
}
return 0;
}

// End of the Project

You might also like