title | date | draft | tags | categories | |||
---|---|---|---|---|---|---|---|
Algorithm4 Java Solution 1.3.42 |
2019-07-28 08:47:27 +0800 |
false |
|
|
Copy a stack. Create a new constructor for the linked-list implementation of Stack so that
Stack<Item> t = new Stack<Item>(s)
; makes t a reference to a new and independent copy of the stack s.
code:
public _Stack(_Stack<Item> another){
_Stack<Item> temp = new _Stack<>();
while (!another.isEmpty()){
temp.push(another.pop());
}
while (!temp.isEmpty()){
Item item = temp.pop();
this.push(item);
another.push(item);
}
}