0% found this document useful (0 votes)
100 views5 pages

Q9) Write A Program in C++ For Stack and Its Applications

The document describes a C++ program to implement a stack and its operations. It defines a stack as a linear data structure that follows LIFO principles. It details the push, pop, and peek operations, providing algorithms for each. It lists applications of stacks such as expression evaluation, backtracking, and memory management. The code provided implements a stack using an array, demonstrating push, pop, and display functions with a driver program to test the stack operations.

Uploaded by

Ayush Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views5 pages

Q9) Write A Program in C++ For Stack and Its Applications

The document describes a C++ program to implement a stack and its operations. It defines a stack as a linear data structure that follows LIFO principles. It details the push, pop, and peek operations, providing algorithms for each. It lists applications of stacks such as expression evaluation, backtracking, and memory management. The code provided implements a stack using an array, demonstrating push, pop, and display functions with a driver program to test the stack operations.

Uploaded by

Ayush Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Q9) Write a program in C++ for Stack and Its Applications.

Aim: To implement C++ program using stack.

Description:

A Stack is a linear data structure that holds a linear, ordered sequence of elements. It is an
abstract data type. A Stack works on the LIFO process (Last In First Out), i.e., the element
that was inserted last will be removed first. To implement the Stack, it is required to maintain
a pointer to the top of the Stack, which is the last element to be inserted because we can
access the elements only on the top of the Stack.

Operation on Stack:

1. PUSH: PUSH operation implies the insertion of a new element into a Stack. A new
element is always inserted from the topmost position of the Stack; thus, we always need to
check if the top is empty or not, i.e., TOP=Max-1 if this condition goes false, it means the
Stack is full, and no more elements can be inserted, and even if we try to insert the element, a
Stack overflow message will be displayed.

Algorithm:

Step-1: If TOP = Max-1

Print “Overflow”

Goto Step 4

Step-2: Set TOP= TOP + 1

Step-3: Set Stack[TOP]= ELEMENT

Step-4: END

2. POP: POP means to delete an element from the Stack. Before deleting an element, make
sure to check if the Stack Top is NULL, i.e., TOP=NULL. If this condition goes true, it
means the Stack is empty, and no deletion operation can be performed, and even if we try to
delete, then the Stack underflow message will be generated.

Algorithm:

Step-1: If TOP= NULL

Print “Underflow”
Goto Step 4

Step-2: Set VAL= Stack[TOP]

Step-3: Set TOP= TOP-1

Step-4: END

3. PEEK: When we need to return the value of the topmost element of the Stack without
deleting it from the Stack, the Peek operation is used. This operation first checks if the Stack
is empty, i.e., TOP = NULL; if it is so, then an appropriate message will display, else the
value will return.

Algorithm:

Step-1: If TOP = NULL

PRINT “Stack is Empty”

Goto Step 3

Step-2: Return Stack[TOP]

Step-3: END

Application of the Stack:


1. A Stack can be used for evaluating expressions consisting of operands and operators.
2. Stacks can be used for Backtracking, i.e., to check parenthesis matching in an expression.
3. It can also be used to convert one form of expression to another form.
4. It can be used for systematic Memory Management.

Syntax:

template <class Type, class Container = deque<Type> > class stack;

Program Code:

#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Output:

You might also like