# 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
the following tasks by using only the ADT stack operations:
a. Display the contents of S in reverse order, that is, display the top last.
b. Count the number of items in S, leaving S unchanged.
c. Delete every occurrence of a specified item from S, leaving the order of the remaining
items unchanged.
7. Suppose that the ADT stack included the method matchAndPop, which matches the top
element with the parameter passed to the method matchAndPop; if it matches, pop the
top entry from the stack.
a. Implement this method for each of the following classes:
i. LinkedStack, as outlined in Listing 6-1.
ii. ArrayStack, as outlined in Listing 6-2.
b. What would be the time efficiency of the methods defined in part a?
```