King Saud University College of Computer and Information Sciences

advertisement
King Saud University
College of Computer and Information Sciences
Information Technology Department
IT212: Data Structures
First Semester 1434/1435
Tutorial # 5
1. Show the contents of the stack as you trace the algorithm checkBalance, given in segment 21.9, for the
following expression: {a*(b+c)}/{(d-e+[f/g)}
2. Using the algorithm convertToPostfix, given in Segment 21.17, convert each of the following infix
expressions to postfix expressions, trace and show the stacks:
a. (a - b * c) / (d * e ^ f + g)
b. a /( b * (c + d) - e)
3. Using the algorithm evaluatePostfix, given in Segment 21.20, evaluate the following postfix expression.
Assume that a = 2, b = 3, c = 4, d = 5, e = 6: e b c a ^ * + d – 6 3 4 2 ^ * + 5 –
4. Show the contents of the two stacks as you trace the algorithm evaluateInfix, given in segment 21.22, for
the following infix expression: (d ^ a - a) * b / (c + 4) * d
Assume that a = 2, b = 3, c = 4, and d = 5
5. The ADT stack lets you peek at its top element without removing it. For some applications of stacks, you
also need to peek at the element beneath the top element without removing it. We will call such an
operation peek2. If the stack has more than one element, peek2 returns the second element from the top
without altering the stack. If the stack has fewer than two elements, peek2 returns null. Write a linked
implementation of a stack that includes a method peek2.
6. Write Java code that displays all the objects in a stack s in the order in which they were pushed onto the
stack. After all the objects are displayed, s should have the same contents as when you started.Suppose
that you read a binary string—that is, a string of 0s and 1s—one character at a time. Write 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 other’s.
7. Write Java code that displays all the objects in a stack s in the order in which they were pushed onto the
stack. After all the objects are displayed, s should have the same contents as when you started.
1
Download