首页 > 代码库 > 算法(Algorithms)第4版 练习 1.3.32

算法(Algorithms)第4版 练习 1.3.32

ADT:

    /**
     * see if Steque is empty
     * @return {@code true} Steque is empty
     * {@code false} Steque isn‘t empty
     */
    public boolean isEmpty()

    /**
     * return the size of the Steque
     * @return return the size of the Steque
     */
    public int size()

    /**
     * push one item at the beginning of the steque
     * 
     * @param item the item to be pushed
     */
    public void push(Item item)

    /**
     * pop one item at the beginning of the steque
     * 
     * @return return the item at the beginning of the steque
     * @throws NoSuchElementException if the steque is empty
     */
    public Item pop()

    /**
     * enqueue one item at the end of the steque
     * @param item the item to be enqueued
     */
    public void enqueue(Item item)

 代码实现以及测试:

package com.qiusongde.creative;

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

import edu.princeton.cs.algs4.StdOut;

public class Steque<Item> implements Iterable<Item> {
    
    private Node<Item> first;
    private Node<Item> last;
    private int size;
    
    public Steque() {
        first = null;
        last = null;
        size = 0;
    }
    
    private class Node<E> {
        E item;
        Node<E> next;
    }
    
    /**
     * see if Steque is empty
     * @return {@code true} Steque is empty
     * {@code false} Steque isn‘t empty
     */
    public boolean isEmpty() {
        return first == null;
    }
    
    /**
     * return the size of the Steque
     * @return return the size of the Steque
     */
    public int size() {
        return size;
    }
    
    /**
     * push one item at the beginning of the steque
     * 
     * @param item the item to be pushed
     */
    public void push(Item item) {
        Node<Item> oldfirst = first;
        
        first = new Node<Item>();
        first.item = item;
        first.next = oldfirst;
        
        if(oldfirst == null) {
            last = first;
        }
        
        size++;
    }
    
    /**
     * pop one item at the beginning of the steque
     * 
     * @return return the item at the beginning of the steque
     * @throws NoSuchElementException if the steque is empty
     */
    public Item pop() {
        if(isEmpty()) 
            throw new NoSuchElementException("Steque is empty");
        
        Item item = first.item;
        first = first.next;
        
        if(isEmpty())
            last = null;
        
        size--;
        
        return item;
    }
    
    /**
     * enqueue one item at the end of the steque
     * @param item the item to be enqueued
     */
    public void enqueue(Item item) {
        Node<Item> oldlast = last;
        
        last = new Node<Item>();
        last.item = item;
        last.next = null;
        
        if(oldlast == null)
            first = last;
        else
            oldlast.next = last;
        
        size++;
    }
    
    @Override
    public String toString() {
        String s = "";
        
        Node<Item> current = first;
        while(current != null) {
            s += current.item + " ";
            current = current.next;
        }
        
        s += first == null ? "first:null " : "first:" + first.item + " ";
        s += last == null ? "last:null " : "last:" + last.item + " ";
        
        return s;
    }

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

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

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

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
        
    }
    
    public static void main(String[] args) {
        Steque<Integer> steque = new Steque<Integer>();
        StdOut.println("Initial steque:");
        StdOut.println(steque);
        StdOut.println();
        
        //test push function
        StdOut.println("Test push function");
        for(int i = 1; i <= 10; i++) {
            steque.push(i);
            StdOut.println("push success: " + i);
            StdOut.println(steque);
        }
        StdOut.println();
        
        //test pop function
        StdOut.println("Test pop function");
        for(int i = 1; i <= 10; i++) {
            int out = steque.pop();
            StdOut.println("pop success: " + out);
            StdOut.println(steque);
        }
        StdOut.println();
        
        //test enqueue function
        StdOut.println("Test enqueue function");
        for(int i = 1; i <= 10; i++) {
            steque.enqueue(i);
            StdOut.println("enqueue success: " + i);
            StdOut.println(steque);
        }
        StdOut.println();
        
        //end
        for(int i : steque) {
            StdOut.print(i + " ");
        }
        StdOut.println();
        
    }

}

 

输出结果:

Initial steque:
first:null last:null 

Test push function
push success: 1
1 first:1 last:1 
push success: 2
2 1 first:2 last:1 
push success: 3
3 2 1 first:3 last:1 
push success: 4
4 3 2 1 first:4 last:1 
push success: 5
5 4 3 2 1 first:5 last:1 
push success: 6
6 5 4 3 2 1 first:6 last:1 
push success: 7
7 6 5 4 3 2 1 first:7 last:1 
push success: 8
8 7 6 5 4 3 2 1 first:8 last:1 
push success: 9
9 8 7 6 5 4 3 2 1 first:9 last:1 
push success: 10
10 9 8 7 6 5 4 3 2 1 first:10 last:1 

Test pop function
pop success: 10
9 8 7 6 5 4 3 2 1 first:9 last:1 
pop success: 9
8 7 6 5 4 3 2 1 first:8 last:1 
pop success: 8
7 6 5 4 3 2 1 first:7 last:1 
pop success: 7
6 5 4 3 2 1 first:6 last:1 
pop success: 6
5 4 3 2 1 first:5 last:1 
pop success: 5
4 3 2 1 first:4 last:1 
pop success: 4
3 2 1 first:3 last:1 
pop success: 3
2 1 first:2 last:1 
pop success: 2
1 first:1 last:1 
pop success: 1
first:null last:null 

Test enqueue function
enqueue success: 1
1 first:1 last:1 
enqueue success: 2
1 2 first:1 last:2 
enqueue success: 3
1 2 3 first:1 last:3 
enqueue success: 4
1 2 3 4 first:1 last:4 
enqueue success: 5
1 2 3 4 5 first:1 last:5 
enqueue success: 6
1 2 3 4 5 6 first:1 last:6 
enqueue success: 7
1 2 3 4 5 6 7 first:1 last:7 
enqueue success: 8
1 2 3 4 5 6 7 8 first:1 last:8 
enqueue success: 9
1 2 3 4 5 6 7 8 9 first:1 last:9 
enqueue success: 10
1 2 3 4 5 6 7 8 9 10 first:1 last:10 

1 2 3 4 5 6 7 8 9 10 

 

算法(Algorithms)第4版 练习 1.3.32