0% found this document useful (0 votes)
6 views

Stack Implementation

Uploaded by

dikshapoptani1
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Stack Implementation

Uploaded by

dikshapoptani1
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5

int top = -1, stack[MAX];


void push()
{
int el;
if(top == MAX-1)
{
printf("\nStack Overflow!");
return;
}
printf("\nEnter the element to be pushed: ");
scanf("%d", &el);
stack[++top] = el;
printf("\nElement pushed!");
}

void pop()
{
int el;
if(top == -1)
{
printf("\nStack Underflow!");
return;
}
el = stack[top--];
printf("%d popped!", el);
}

void display()
{
int i;
if(top == -1)
{
printf("\nStack is empty, cannot display anything!");
return;
}
printf("\nStack Contains: \n");
for(i=0; i<=top; i++)
printf("%d\n", stack[i]);
}

void main()
{
int choice;
clrscr();
while(1)
{
printf("\n-----MENU-----");
printf("\n1. Push \n2. Pop \n3. Display \n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice entered!");
break;
}
}
}

You might also like