Asd3 en tw2
Asd3 en tw2
Exercise 2:
Give the output printed by java Stack for the input
it was - the best - of times - - - it was - the - -
Exercise 3:
Suppose that an intermixed sequence of (stack) push and pop operations are performed. The pushes
push the integers 0 through 9 in order; the pops print out the return value. Which of the following
sequence(s) could not occur?
(a) 4 3 2 1 0 9 8 7 6 5
(b) 4 6 8 7 5 3 2 9 0 1
(c) 2 5 6 7 4 8 9 3 1 0
(d) 4 3 2 1 0 5 6 7 8 9
(e) 1 2 3 4 5 6 9 8 7 0
(f) 0 4 6 5 3 8 1 7 2 9
(g) 1 4 7 9 8 6 5 3 0 2
(h) 2 1 4 3 6 5 8 7 9 0
Exercise 4:
Write a stack client Parentheses.java that reads in sequence of left and right parentheses, braces, and brackets
from standard input and uses a stack to determine whether the sequence is properly balanced. For example,
your program should print true for [()]{}{[()()]()} and false for [(]).
Exercise 5:
What does the following code fragment print when n is 50? Give a high-level description of what it does
when presented with a positive integer n.
Stack<Integer> s = new Stack<Integer>();
while (n > 0) {
s.push(n % 2);
n = n / 2;
}
while (!s.isEmpty())
System.out.print(s.pop());
System.out.println();
Page 1/2
Exercise 6:
What does the following code fragment do to the queue q?
Stack<String> s = new Stack<String>();
while(!q.isEmpty())
s.push(q.dequeue());
while(!s.isEmpty())
q.enqueue(s.pop());
Exercise 7:
Add a method peek to Stack.java that returns the most recently inserted item on the stack (without popping it).
Exercise 8:
Write a filter Program InfixToPostfix.java that converts an arithmetic expression from infix to postfix.
Exercise 9:
Write a program EvaluatePostfix.java that that takes a postfix expression from standard input, evaluates it, and
prints the value. (Piping the output of your program from the previous exercise to this program gives equivalent
behavior to Evaluate.java.)
Exercise 10:
Suppose that a client performs an intermixed sequence of (queue) enqueue and dequeue operations. The
enqueue operations put the integers 0 through 9 in order onto the queue; the dequeue operations print
out the return value. Which of the following sequence(s) could not occur?
(a) 0 1 2 3 4 5 6 7 8 9
(b) 4 6 8 7 5 3 2 9 0 1
(c) 2 5 6 7 4 8 9 3 1 0
(d) 4 3 2 1 0 5 6 7 8 9
Exercise 11:
Develop a class ResizingArrayQueueOfStrings that implements the queue abstraction with a fixed-
size array, and then extend your implementation to use array resizing to remove the size restriction.
Exercise 12:
Queue with two stacks. Implement a queue with two stacks so that each queue operations takes a constant
amortized number of stack operations. Hint: If you push elements onto a stack and then pop them all, they
appear in reverse order. If you repeat this process, they're now back in order.
Exercise 13:
Stack with a queue. Implement a stack with a single queue so that each stack operations takes a linear number
of queue operations. Hint: to delete an item, get all of the elements on the queue one at a time, and put them
at the end, except for the last one which you should delete and return. (Admittedly very inefficient.)
Exercise 14:
Two stacks with a deque. Implement two stacks with a single deque so that each operation takes a constant
number of deque operations.
Page 2/2