算法第四版1.3背包、队列和栈:习题1.3.47

Catenable Queues

import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;

public class CatenableQueue implements Iterable {
    public static void main(String[]args){
        CatenableQueue priorQueue=new CatenableQueue<>();
        Queue lastQueue=new Queue<>();
        for (int i=0;i<5;i++){
            priorQueue.enqueue((char)('a'+i));
            lastQueue.enqueue((char)('a'+5+i));
        }
        priorQueue.catenate(lastQueue);
        for (char c:priorQueue)
            StdOut.print(c+" ");
        StdOut.println();
        for (char c:lastQueue)
            StdOut.print(c+" ");
        StdOut.println();
    }

    private Node first;
    private Node last;
    private int N;
    private class  Node{
        Item item;
        Node next;
    }
    public boolean isEmpty(){return first==null;}
    public int size(){return N;}
    public void enqueue(Item item){
        Node newlast=new Node();
        newlast.item=item;
        if (isEmpty()){
            first=newlast;
            last=newlast;
        }
        else {
            last.next=newlast;
            last=newlast;
        }
        N++;
    }
    public Item dequeue(){
        if (isEmpty())return null;
        Item item=first.item;
        first=first.next;
        N--;
        return item;
    }
    public void catenate(Queue lastQueue){
        for (Item item:lastQueue)
            enqueue(item);
    }

    public Iterator iterator(){
        return new CatenableQueueIterator();
    }
    private class CatenableQueueIterator implements Iterator{
        private Node current=first;
        public boolean hasNext(){return current!=null;}
        public Item next(){
            Item item=current.item;
            current=current.next;
            return item;
        }
    }
}

Catenable Stacks

import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;

public class CatenableStack  implements Iterable{
    public static void main(String[]args){
        CatenableStack priorStack=new CatenableStack<>();
        Stack lastStack=new Stack<>();
        for (int i=0;i<5;i++){
            priorStack.push((char)('a'+i));
            lastStack.push((char)('a'+5+i));
        }
        priorStack.catenate(lastStack);
        for (char c:priorStack)
            StdOut.print(c+" ");
        StdOut.println();
        for (char c:lastStack)
            StdOut.print(c+" ");
        StdOut.println();
    }

    private Node first;
    private Node last;
    private int N;
    private class Node{
        Item item;
        Node next;
    }
    public boolean isEmpty(){return first==null;}
    public int size(){return  N;}
    public void push(Item item){
        Node newfirst=new Node();
        newfirst.item=item;
        if (isEmpty()){
            first=newfirst;
            last=newfirst;
        }else {
            newfirst.next=first;
            first=newfirst;
        }
        N++;
    }
    public Item pop(){
        if (isEmpty())return null;
        Item item=first.item;
        first=first.next;
        N--;
        return item;
    }
    public void catenate(Stack lastStack){
        Stack temp=new Stack<>();
        int length=lastStack.size();
        for (int i=0;i iterator(){
        return new CatenableQueueIterator();
    }
    private class CatenableQueueIterator implements Iterator{
        private Node current=first;
        public boolean hasNext() {
            return current!=null;
        }
        public Item next(){
            Item item=current.item;
            current=current.next;
            return item;
        }
    }
}

 

 

 

 

你可能感兴趣的:(算法第四版1.3背包,队列和栈课后习题)