ch03 - 1.1 Assume that the stack names is defined as in...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1.1 Assume that the stack names is defined as in Figure 3.2(c) and perform the following sequence of operations. Indicate the result of each operation and show the new stack if it is changed. names.push("Jane"); names.push("Joseph"); String top = names.pop(); String nextTop = names.peek(); Philip Dustin Robin Debbie Rich names.push("Jane"); Jane Philip Dustin Robin Debbie Rich names.push("Joseph"); Joseph Jane Philip Dustin Robin Debbie Rich String top = names.pop(); Jane Philip Dustin Robin Debbie Rich The String top contains "Joseph" String nextTop = names.peek(); The String nextTop contains "Jane" and the stack remains unchanged. 1.2 For the stack names in Figure 3.2(c), what is the effect of the following: while (!names.empty()) { System.out.printIn(names.pop()); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The following would be output Philip Dustin Robin Debbie Rich 1.3 What would be the effect of using peek instead of pop in Question 2? The program would be in an infinite loop printing “ Philip ”. 2.1 The result returned by the palindrome finder depends on all characters in a string, including spaces and punctuation. Discuss how you would modify the palindrome finder so that only the letters in the input string were used to determine whether the input string was a palindrome. You should ignore any other characters. The palindrome finder could be modified to only push letters onto the stack and to append only the letter characters to a new temporary string being formed. Then, after the reverse string is built by emptying the stack, it could be compared to the temporary string. 2.2 Trace the execution of method isBalanced for each of the following expressions. Your trace should show the stack after each push or pop operation. Also show the values of balanced , isOpen , and isClose after each closing parenthesis is processed. (a + b * {c / [d - e]}) + (d * [e - f]) (a + b * {c / [d - e}}) + (d / e) (w * (x + y))) - 2 * z (a + b * {c / [d - e]}) + (d * [e - f]) After push('(') stack is ( After push('{') stack is { ( After push('[') stack is [ { (
Background image of page 2
After '[' = pop() stack is { ( OPEN.indexOf([) == 1 CLOSE.indexOf(]) == 1 balanced == true After '{' == pop() stack is ( OPEN.indexOf({) == 2 CLOSE.indexOf(}) == 2 balanced == true After '(' = pop() stack is empty OPEN.indexOf(() == 0 CLOSE.indexOf()) == 0 balanced == true After push('(') stack is ( After
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

ch03 - 1.1 Assume that the stack names is defined as in...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online