# Sheet 5 (Tutorial)

```King Saud University
College of Computer and Information Sciences
Information Technology Department
First Semester 1435/1436
IT212: Data Structures
Sheet 5 (Tutorial)
1. Show the contents of the stack as you trace the algorithm checkBalance, given in segment
5.8, for the following expression: {a {b * (c – d)} / (e + f - g) / h)] - i}
State whether it will return true or false.
2. Using the algorithm convertToPostfix, given in Segment 5.16, convert each of the following
infix expressions to postfix expressions; trace and show the stacks:
i.
ii.
(a + b) * c / (d + (e - f))
(a * (b + c) ^ d – e) * f + g ^ h
3. Using the algorithm evaluatePostfix, given in Segment 5.18, evaluate the following postfix
expression: a e * d a - / a e * +
Assume that a = 2, b = 3, c = 4, d = 5, e = 1
4. Show the contents of the two stacks as you trace the algorithm evaluateInfix, given in
segment 5.21, for the following infix expression: (c * a - 2) / e * (d ^ b - e * c - 2) -87
Assume that a = 2, b = 3, c = 4, d = 5, e=6, and f=1.
5. Suppose that you read a binary string—that is, a string of 0s and 1s—one character at a
time. Write a Java method that uses a stack but no arithmetic to see whether the number of
0s is equal to the number of 1s. When these counts are not equal, state how you could tell
which character—0 or 1—occurs most frequently and by how much its count exceeds the
others.
6. Suppose you have a stack S and an empty auxiliary stack T. Show how you can do each of