0% found this document useful (0 votes)
33 views6 pages

DSAL3

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

DSAL3

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

Lab Journal – Lab 2

Data Structures and Algorithms

Lab Journal - Lab 2


Name: Abdurrehman Ranjha

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 :

Give answers to the following.

1. Convert (manually) the following expressions to postfix.

(A+B*D)/(E-F)+G :

A*(B+D)/E-F*(G+H/K) :

Data Structures and Algorithms Page 1


Lab Journal – Lab 2

2. Convert the following infix expressions to prefix.

A*B +(C/E) – (F+G) :

A+(B-D)/E-F*(G*H+K)

Data Structures and Algorithms Page 2


Lab Journal – Lab 2

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

Symbol Stack Contents Output Expression

( (

A ( A

+ + A

B + AB

) AB+

* * AB+

C * AB+C

- - AB+C*

D - AB+C*D

Data Structures and Algorithms Page 3


Lab Journal – Lab 2

+ + 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+*+

Symbol Stack Contents

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

Data Structures and Algorithms Page 4


Lab Journal – Lab 2

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";
}

Data Structures and Algorithms Page 5


Lab Journal – Lab 2

void reverse() {
cout << "Enter the word you want to reverse:";

cin >> a;
length = a.length();

for (int i = 0; i < length; i++) {


arr[i] = a[i];
push(arr[i]);

}
cout << "\nreversed string:";
while (!isempty()) {
pop();
}
}
};

int main() {
stack a;
a.reverse();

+++++++++++++++++++++++++

Data Structures and Algorithms Page 6

You might also like