With respect to space, it's better to use a singly-linked list because it has half the overhead of a doubly-linked list. With respect to time, it's better to use a double-linked list because insertions and deletions can occur at either end of the deque, and a singly-linked list requires a full traversal of the deque for a deletion at the down-stream end.
void f(Stack<?> s) { ... }
and
void f(Stack<Object> s) { ... }
The first declaration is a generic method, capable of accepting a stack of any type as a parameter value. The second declaration is a non-generic method that can accept only a stack of ojects as a parameter value.
1 2 3 4
and writes the numbers 4 3 2 1
. Is it
possible to write a program that reads 1 2 3 4
and writes (1 4 2 3)? If
so, show the program; if not, explain why no such program is possible.
It's not possible. The only place to store 2
and 3
while 4
is
being read and printed is on the stack, with 2
being pushed before 3
.
Once 4
is printed, it's time to print 2
, but the only number
available for printing is 3
, the top of the stack.
See Laforge, table 4.10, page 159.
This page last modified on 15 October 2009.