Skip to content

Latest commit

 

History

History
33 lines (21 loc) · 1.05 KB

Ex_1_3_12.md

File metadata and controls

33 lines (21 loc) · 1.05 KB
title date draft tags categories
Algorithm4 Java Solution 1.3.12
2019-07-04 05:47:10 +0800
false
JAVA
TECH
archives

1.3.12

Problem:

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.

Solution:

Stack client means you should not change the original api of Stack.

2019-07-13-001

solution:

  1. Stack has list iterator, it iterates from the latest added element to end.

  2. 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.

Reference:

YangXiaoHei_1-3-12