算法(Algorithms)第4版 练习 1.3.29

代码实现:

//1.3.29
package com.qiusongde.linkedlist;

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

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

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

    private Node last;
    private int n;
    
    private class Node {
        Item item;
        Node next;
    }
    
    public CircularQueue() {
        last = null;
        n = 0;
    }
    
    public boolean isEmpty() {
        return last == null;
    }
    
    public int size() {
        return n;
    }
    
    public void enqueue(Item item) {
        
        Node oldlast = last;//save oldlast
        
        last = new Node();//new node
        last.item = item;
        if(oldlast == null) {//empty
            last.next = last;
        }
        else {
            last.next = oldlast.next;//next is first
            oldlast.next = last;
        }
        
        n++;
    }
    
    public Item dequeue() {
        if(isEmpty())
            throw new NoSuchElementException("Queue is empty");
        
        Item item = last.next.item;
        
        if(last.next == last) {//only one node
            last = null;
        } else {
            last.next = last.next.next;
        }
        
        n--;
                
        return item;
    }

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

        //it is important that this class cannot change the CircularQueue
        //so we can only change precurrent and can't change precurrent.next
        private Node precurrent;
        
        public CircularQueueIterator() {
            precurrent = last;
        }
        
        @Override
        public boolean hasNext() {
            return precurrent != null;
        }

        @Override
        public Item next() {
            if(!hasNext())
                throw new NoSuchElementException("Queue is empty");
            
            Item item = precurrent.next.item;
            
            if(last == last.next) {//one node
                precurrent = null;//end
            } else {
                precurrent = precurrent.next;
                if(precurrent == last) {//precurrent equals last again
                    precurrent = null;//end
                }
            }
                
            return item;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
        
    }
    
    public static void main(String[] args) {
        
        CircularQueue<String> queue = new CircularQueue<String>();
        StdOut.println("Initialized size:" + queue.size());
        
        while (!StdIn.isEmpty()) {
            
            String item = StdIn.readString();
            
            if (!item.equals("-")) {
                
                queue.enqueue(item); 
                StdOut.println("enqueue success:" + item + " size:" + queue.size());
                
                StdOut.print("Left on queue: ");
                for (String s : queue) {
                    StdOut.print(s + " ");
                }
                StdOut.println();
                
            } else {
                if(queue.isEmpty())
                    StdOut.println("dequeue error, queue empty");
                else {
                    StdOut.println("dequeue success:" + queue.dequeue() + " size:" + queue.size());
                    
                    StdOut.print("Left on queue: ");
                    for (String s : queue) {
                        StdOut.print(s + " ");
                    }
                    StdOut.println();
                }
            }
            
        }
        
    }

}

测试数据;

to
be
or
not
to
-
be
-
-
that
-
-
-
is

 

输出结果:

Initialized size:0
enqueue success:to size:1
Left on queue: to 
enqueue success:be size:2
Left on queue: to be 
enqueue success:or size:3
Left on queue: to be or 
enqueue success:not size:4
Left on queue: to be or not 
enqueue success:to size:5
Left on queue: to be or not to 
dequeue success:to size:4
Left on queue: be or not to 
enqueue success:be size:5
Left on queue: be or not to be 
dequeue success:be size:4
Left on queue: or not to be 
dequeue success:or size:3
Left on queue: not to be 
enqueue success:that size:4
Left on queue: not to be that 
dequeue success:not size:3
Left on queue: to be that 
dequeue success:to size:2
Left on queue: be that 
dequeue success:be size:1
Left on queue: that 
enqueue success:is size:2
Left on queue: that is

 

posted @ 2017-03-06 19:49  我是老邱  阅读(415)  评论(0编辑  收藏  举报