title | date | draft | tags | categories | |||
---|---|---|---|---|---|---|---|
Algorithm4 Java Solution 1.3.12 |
2019-07-04 05:47:10 +0800 |
false |
|
|
Write an iterable Stack client that has a static method copy() that takes a stack of strings as argument and returns a copy of the stack. Note : This ability is a prime example of the value of having an iterator, because it allows development of such functionality without changing the basic API.
Stack client
means you should not change the original api of Stack
.
solution:
-
Stack has list iterator, it iterates from the latest added element to end.
-
If we want a copy of a stack, we should have two stacks. iterate throw the input stack, push each element into stack_1, then iterates stack_1, push each element to stack_2. Then we get the right answer.