Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 744 Bytes

Ex_1_3_42.md

File metadata and controls

42 lines (31 loc) · 744 Bytes
title date draft tags categories
Algorithm4 Java Solution 1.3.42
2019-07-28 08:47:27 +0800
false
JAVA
TECH
archives

1.3.42

Problem:

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.

Solution:

code:

Ex_1_3_42.java

    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);
      }
    }

Reference: