算法-第四版-练习1.3.29解答

问题

用环形链表实现Queue。环形链表也是一条链表,只是没有任何结点链接为空,且只要链表非空则last.next的值就为first。只能使用一个Node类型的实例变量(last)。

解决思路

出列时将last.next指向last.next.next。入列时需要修改两条链接才能真正的让一个结点加入到一个环中。最后在移到last的位置。

待优化,出列后的结点应该修改其next为null。

代码

/**
 * Description : 
 * Author      : mn@furzoom.com
 * Date        : Oct 25, 2016 11:39:46 AM
 * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
 */
package com.furzoom.lab.algs.ch103;

import java.util.Iterator;

/**
 * ClassName    : CircleQueue <br>
 * Function     : TODO ADD FUNCTION. <br>
 * date         : Oct 25, 2016 11:39:46 AM <br>
 * 
 * @version 
 */
public class CircleQueue<Item> implements Iterable<Item>
{
    private Node last;
    
    private class Node
    {
        public Item item;
        public Node next;
    }
    
    public CircleQueue()
    {
        last = null;
    }
    
    public boolean isEmpty()
    {
        return last == null;
    }
    
    public Item dequeue()
    {
        if (isEmpty())
            return null;
        
        Item item = last.next.item;
        if (last.next == last)
        {
            last = null;
        }
        else
        {
            last.next = last.next.next;
        }
        return item;
    }
    
    public void enqueue(Item item)
    {
        Node node = new Node();
        node.item = item;
        if (last == null)
        {
            last = node;
            node.next = node;
        }
        else {
            node.next = last.next;
            last.next = node;
            last = node;
        }
    }
    
    @Override
    public Iterator<Item> iterator()
    {
        return new Iter();
    }
    
    private class Iter implements Iterator<Item>
    {
        private Node first;
        private boolean one;
        
        public Iter()
        {
            if (last == null)
            {
                first = null;
            }
            else 
            {
                first = last.next;
                one = (last == last.next);
            }
        }
        @Override
        public boolean hasNext()
        {
            // if
            if (last == last.next)
            {
                if (one)
                {
                    one = false;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return first != null;
            }
        }
        @Override
        public Item next()
        {
            Item item;
            if (last == last.next)
            {
                first = null;
                item = last.item;
            }
            else
            {
                item = first.item;
                first = first.next;
                if (first == last.next)
                {
                    first = null;
                }
            }
            return item;
        }
    }
}

测试代码:

/**
 * Description : 
 * Author      : mn@furzoom.com
 * Date        : Oct 25, 2016 11:38:20 AM
 * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
 */
package com.furzoom.lab.algs.ch103;

import java.util.Iterator;

/**
 * ClassName    : E10329 <br>
 * Function     : TODO ADD FUNCTION. <br>
 * date         : Oct 25, 2016 11:38:20 AM <br>
 * 
 * @version 
 */
public class E10329
{
    public static void main(String[] args)
    {
        CircleQueue<String> queue = new CircleQueue<String>();
        queue.enqueue("a");
        queue.enqueue("b");
        queue.enqueue("c");
        queue.enqueue("d");
        queue.enqueue("e");
        
        Iterator<?> it = queue.iterator();
        while (it.hasNext())
        {
            System.out.println(it.next());
        }
        System.out.println("dequeue: ");
        String s;
        while ((s = queue.dequeue()) != null)
        {
            System.out.println(s);
        }
    }
    
}


结果:

a
b
c
d
e
dequeue: 
a
b
c
d
e


算法-第四版-1.3 背包、队列和栈-习题索引汇总

算法-第四版习题索引汇总


posted @ 2017-10-22 15:50  枫竹梦  阅读(159)  评论(0编辑  收藏  举报