【问题标题】:Java class implementing first-in-first-out queueJava类实现先进先出队列
【发布时间】:2017-04-24 13:31:27
【问题描述】:

我有一个即将到来的考试,正在努力解决这个问题,希望有人能帮忙。

提供一个完整的实现接口的Java类

interface StringQueue 
{ boolean isEmpty(); 
void add(String c); 
String front(); 
void removeFront(); 
} 

该类应提供标准先进先出队列的实现。队列中的字符应存储在使用 QueueCell 类型的对象构造的单链表中;你必须把这个类写成一个内部类。 (您不得使用 Collections Framework 中的 LinkedList 类)。 front 和 removeFront 方法在应用于空队列时应该抛出 QueueException 类型的异常;你可以假设 QueueException 类已经被编写好了。

提前致谢

【问题讨论】:

  • 你的问题是什么?这只是一个要求列表
  • 我认为它不会帮助您查看实现。当然,它不会帮助您学习一个实现。即将到来的考试不太可能要求您实施堆栈……如果过去的考试这样做的话。
  • 那么您的问题是什么?通过谷歌搜索,您可以找到 FIFO 如何工作的示例。写两个方法,第一个返回第一个元素并删除它,第二个将新元素放在列表末尾的方法很难吗?
  • 如果 String 是 QueueCell 类型,你想如何添加它?

标签: java class queue


【解决方案1】:

简单的尝试

实施:

 private LinkedList<E> list = new LinkedList<E>();
 public void add(E item) {
    list.addLast(item);
 }
 public E removeFront() {
    return list.poll();
 }
 public boolean isEmpty() {
    return !list.isEmpty();
 }
 public int size() {
    return list.size();
 }
 public void addItems(GenQueue<? extends E> q) {
    while (q.hasItems()) list.addLast(q.dequeue());
 }

【讨论】:

  • You must not use the LinkedList class from the Collections Framework 的哪一部分你不明白?
【解决方案2】:

我建议您查看我的实现并尝试编写自己的实现。不然你复制粘贴也没有用。

界面

public interface StringQueue {
    boolean isEmpty();
    void add(String c);
    String front();
    void removeFront();
}

实施:

public class StringQueueImpl implements StringQueue {
    private QueueCell head;
    private int size = 0;

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void add(String c) {
        if(head == null){
            head = new QueueCell(c);
        } else {
            QueueCell current = head;
            while(current != null){
                if(current.next == null){
                    current.next = new QueueCell(c);
                    break;
                } else {
                    current = head.next;
                }
            }
        }
        size++;
    }

    @Override
    public String front() throws QueueException {
        if(size == 0 || head == null){
            throw new QueueException();
        }
        return head.value;
    }

    @Override
    public void removeFront() throws QueueException {
        if(size == 0 || head == null){
            throw new QueueException();
        }

        if(head.next != null){
            head = head.next;
        } else { // if only 1 element is in the queue
            head = null;
        }
        size--;
    }

    private static class QueueCell {
        private QueueCell next;
        private String value;

        QueueCell(String s){
            value = s;
        }
    }

    public static class QueueException extends RuntimeException {
        public QueueException(){
            super("Queue is empty");
        }
    }
}

【讨论】:

  • 你将如何实现 isEmpty ?
  • @John 只是一个带有return size == 0return head == nullboolean 方法,任何一个都可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多