You are on page 1of 7

Lab Manual

CSC-216(L8) – Data Structures


(Prepared by Haji Gul)

Course Objective
o Implement various data structures and their algorithms, and apply them in implementing simple applications.
o Analyze simple algorithms and determine their complexities.
o Apply the knowledge of data structures to other application domains.
o Design new data structures and algorithms to solve problems

Class Policy
1. A student must reach the class-room in time. Students late than 8 mins may join the class but are not entitled to be marked
present.
2. Attendance shall be marked at the start of the class and students failing to secure 75% attendance will not be allowed to sit
in the Final Exam.
3. Those who are absent on the announcement date of the assignment/test, must get the topic/chapter of the test/assignment
confirmed through their peers
Order of Operators:

Infix to Postfix
Solution:
o (A+B)*C/D
 AB + * C/D, suppose AB+ = p
 P * C/D
 PC* /D , suppose (PC* = q)
 Q / D;
 QD/
 PC* D (putting values )
 AB+C*D/ (putting values)

Infix to Prefix Conversion


Given expression is: (A*B) + C
Solution: Three steps are included below, we will follow these three to solve this problem.
o Revers the expression
o Find postfix
o Reverse the postfix expression

Step No.1:
Convert the current expression into reverse order.
C+ (B*A)
Step No.2:
Infix to postfix conversion
Symbol Stack Postfix
C - C
+ + C
( +( C
B +( CB
* +(* CB
A +(* CBA
) + CBA*
CBA*+

CBA*+, is a postfix expression.

Step No.3:
Revers the postfix expression, that will be the final answer and prefix expression.
+*ABC

Evaluation of Postfix Expressions Using Stack

As discussed in Infix to Postfix Conversion, the compiler finds it convenient to evaluate an


expression in its postfix form. The virtues of postfix form include elimination of parentheses which
signify priority of evaluation and the elimination of the need to observe rules of hierarchy,
precedence and associativity during evaluation of the expression.

As Postfix expression is without parenthesis and can be evaluated as two operands and an operator
at a time, this becomes easier for the compiler and the computer to handle.

Evaluation rule of a Postfix Expression states:

o While reading the expression from left to right, push the element in the stack if it is an
operand.
o Pop the two operands from the stack, if the element is an operator and then evaluate it.
o Push back the result of the evaluation. Repeat it till the end of the expression.

Let's see an example to better understand the algorithm:

o Expression: 456*+
Postfix Evaluation C++ Program

#include<iostream>

#include<cmath>
#include<stack>

using namespace std;

float scanNum(char ch) {

int value;

value = ch;

return float(value-'0'); //return float from character

int isOperator(char ch) {

if(ch == '+'|| ch == '-'|| ch == '*'|| ch == '/' || ch == '^')

return 1; //character is an operator

return -1; //not an operator

int isOperand(char ch) {

if(ch >= '0' && ch <= '9')

return 1; //character is an operand

return -1; //not an operand

float operation(int a, int b, char op) {

//Perform operation

if(op == '+')

return b+a;

else if(op == '-')

return b-a;

else if(op == '*')

return b*a;

else if(op == '/')


return b/a;

else if(op == '^')

return pow(b,a); //find b^a

else

return INT_MIN; //return negative infinity

float postfixEval(string postfix) {

int a, b;

stack<float> stk;

string::iterator it;

for(it=postfix.begin(); it!=postfix.end(); it++) {

//read elements and perform postfix evaluation

if(isOperator(*it) != -1) {

a = stk.top();

stk.pop();

b = stk.top();

stk.pop();

stk.push(operation(a, b, *it));

}else if(isOperand(*it) > 0) {

stk.push(scanNum(*it));

return stk.top();

main() {

string post = "53+62/*35*+";


cout << "The result is: "<<postfixEval(post);

You might also like