Program 6
Program 6
OBJECTIVE
Perform push, pop, and display function on the stack implemented by array.
THEORY
In this program we take an array of constant size that is 100. We use top pointer to implement
stack which represent the topmost element of the stack. If top=size-1, the message says Overflow
condition, that is no more elements can be added, else if top=0, the message says Underflow
condition, that is, no more elements can be deleted.
PSEUDO CODE
ALGORITHM MainFunction()
BEGIN:
READ(stack[size])
READ(choice)
IF choice==1 THEN
READ(value)
CALL Push(value)
CALL Pop()
CALL Display()
ELSE
WRITE(Invalid Choice)
END;
ALGORITHM Push(value)
BEGIN:
IF top<size-1 THEN
top=top+1
Stack[top]=value
WRITE(stack[top])
ELSE
WRITE(Stack Overflow)
END;
ALGORITHM Pop()
BEGIN:
IF top==-1 THEN
WRITE(Stack Underflow)
ELSE
top=top-1
END;
ALGORITHM Display()
BEGIN:
IF top>=0 THEN
WRITE(stack[])
ELSE
WRITE(Stack is empty)
END;
SOURCE CODE
#include<stdio.h>
int stack[size];
int top=-1;
if(top<size-1){
stack[++top]=value;
printf("stack[%d]=%d",top,value);
else
printf("Stack Overflow");
void pop(){
if(top==-1)
printf("Stack Underflow");
else
top--;
void display(){
if(top>=0){
for(int i=0;i<=top;i++){
printf("%d\n",stack[i]);
}
}
else{
printf("Stack is empty");
int main(){
int choice;
printf("Type 1 if you want to push, 2 if you want to pop, and 3 if you want to display the stack
elements: ");
scanf("%d",&choice);
switch(choice){
case 1:
int value=0;
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("Invalid Input");
}
return 0;
OUTPUT
Type 1 if you want to push, 2 if you want to pop, and 3 if you want to display the stack
elements:1
Stack[1]=5