DSAL3
DSAL3
Enrollment #: 01-134232-021
Class/Section: 3-B
Objective
This lab is intended to introduce students to Stacks and their applications. The students will
implement the Stack and employ it in solving the given problems.
Task 1 :
(A+B*D)/(E-F)+G :
A*(B+D)/E-F*(G+H/K) :
A+(B-D)/E-F*(G*H+K)
3. Convert the following expression from infix to postfix and show the contents of Stack and
the output expression at each step.
(A+B) * C – D+F*G
( (
A ( A
+ + A
B + AB
) AB+
* * AB+
C * AB+C
- - AB+C*
D - AB+C*D
+ + AB+C*D-
F + AB+C*D-F
* + AB+C*D-F
G AB+C*D-FG*+
4. Evaluate the given Postfix expression and trace the contents of the Stack at each step
using the standard evaluation algorithm.
273-/215+*+
2 2
7 2,7
3 2,7,3
- 2,4
/ 0.5
2 0.5,2
1 0.5,2,1
5 0.5,2,1,5
+ 0.5,2,6
* 0.5,12
+ 12.5
Result : 12.5
Task 2 :
Use the above stack class given in lab manual to write a function to reverse a string.
For Example:
alpha->ahpla
right -> thgir
Code :
#include<iostream>
#include<string>
using namespace std;
class stack {
int length;
string a;
char * arr;
int top;
public:
stack() {
top = -1;
arr = new char[length];
}
bool isfull() {
if (top > length) {
return true;
}
else return false;
}
bool isempty() {
if (top == -1) {
return true; }
else return false;
}
void push(char a) {
if (isfull()) { cout << "stack is full"; }
else
{
top++;
arr[top] = a;
}
}
void pop() {
if (!isempty()) {
cout << arr[top--];
}
else cout << "The array is empty";
}
void reverse() {
cout << "Enter the word you want to reverse:";
cin >> a;
length = a.length();
}
cout << "\nreversed string:";
while (!isempty()) {
pop();
}
}
};
int main() {
stack a;
a.reverse();
+++++++++++++++++++++++++