More Related Content
Similar to Stack using Array (20)
More from Sayantan Sur (6)
Stack using Array
- 2. The Main Module
#include<stdio.h>
#include<stdlib.h>
void push(), pop(), display();
int top=-1,stack[5];
int main()
{
int choice;
while(1)
{
printf("n----Stack Menu----n");
printf("1. Push.n");
printf("2. Pop.n");
printf("3. Display.n");
printf("4. Exit.n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("Invalid option.");
}
}
}
- 3. Insert An Element Into Stack
void push()
{
if(top<4)
{
int num;
printf("Enter an integer value: ");
scanf("%d",&num);
top++;
stack[top]=num;
}
else
{
printf("nStack Overflow.n");
}
display();
}
- 4. Delete An Element From Stack
void pop()
{
if(top>=0)
{
printf("Item popped is: %d",stack[top]);
top--;
}
else
{
printf("nStack Underflow.n");
}
display();
}
- 5. Display The Elements In Stack
void display()
{
int head=top;
if(head>=0)
{
printf("The items in the stack are: ");
while(head!=-1)
{
printf("n%d",stack[head]);
head--;
}
}
else
{
printf("nStack empty.n");
}
}