算法(Algorithms)第4版 练习 1.3.12

方法实现:

package com.qiusongde;

import java.util.Iterator;
import java.util.NoSuchElementException;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class Stack<Item> implements Iterable<Item> {

    private Node first;
    private int n;
    
    private class Node {
        Item item;
        Node next;
    }
    
    //1.3.12
    public static Stack<String> copy(Stack<String> stack) {
        Stack<String> temp = new Stack<String>();
        Stack<String> result = new Stack<String>();
        
        for(String s : stack) {
            temp.push(s);
        }
        for(String s : temp) {
            result.push(s);
        }
            
        return result;
    }
    
    public Stack() {
        first = null;
        n = 0;
    }
    
    public void push(Item item) {
        Node oldfirst = first;
        
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        
        n++;
    }
    
    public Item pop() {
        
        if(isEmpty()) 
            throw new NoSuchElementException("Stack is empty");
        
        Item item = first.item;
        first = first.next;
        n--;
        
        return item;
    }
    
    //1.3.7
    public Item peek() {
        if(isEmpty())
            throw new NoSuchElementException("Stack is empty");
        
        return first.item;
    }
    
    public boolean isEmpty() {
        return first == null;
    }
    
    public int size() {
        return n;
    }

    @Override
    public Iterator<Item> iterator() {
        
        return new StackIterator();
    }
    
    private class StackIterator implements Iterator<Item> {

        private Node current = first;
        
        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Item next() {
            if(!hasNext())
                throw new NoSuchElementException("Stack is empty");
            
            Item item = current.item;
            current = current.next;
            
            return item;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Stack don't support remove");
        }
        
    }
    
    public static void main(String[] args) {
        
        Stack<String> stack = new Stack<String>();
        StdOut.println("Initialized size:" + stack.size());
        
        while (!StdIn.isEmpty()) {
            
            String item = StdIn.readString();
            
            if (!item.equals("-")) {
                
                stack.push(item); 
                StdOut.println("push success:" + item + " size:" + stack.size());
                
                StdOut.print("Left on stack: ");
                for (String s : stack) {
                    StdOut.print(s + " ");
                }
                StdOut.println();
                
            } else {
                if(stack.isEmpty())
                    StdOut.println("pop error, stack empty");
                else {
                    StdOut.println("pop success:" + stack.pop() + " size:" + stack.size());
                    
                    StdOut.print("Left on stack: ");
                    for (String s : stack) {
                        StdOut.print(s + " ");
                    }
                    StdOut.println();
                }
            }
            
        }
        
        //1.3.12
        Stack<String> copy = Stack.copy(stack);
        StdOut.println("copy stack:");
        for(String s : copy) {
            StdOut.print(s + " ");
        }
        StdOut.println();
        
    }
    
}

结果输出:

Initialized size:0
to
push success:to size:1
Left on stack: to 
be
push success:be size:2
Left on stack: be to 
or
push success:or size:3
Left on stack: or be to 
not
push success:not size:4
Left on stack: not or be to 
to
push success:to size:5
Left on stack: to not or be to 
copy stack:
to not or be to
原文地址:https://www.cnblogs.com/songdechiu/p/6513837.html