⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myqueue.java

📁 A simple implemantation of Queue in Java
💻 JAVA
字号:
package exoryxigui;
import java.util.*;
import java.util.NoSuchElementException;

public class MyQueue<Node> implements Iterable{
    private int N;         
    private Node_Queue first;    
    private Node_Queue last;     

    private class Node_Queue {
        private Node item;
        private Node_Queue next;
    }

    public MyQueue() {
        first = null;
        last  = null;
    }

    public boolean isEmpty() { return first == null; }
    public int length()      { return N;             }
    public int size()        { return N;             }

    public void enqueue(Node item) {
        Node_Queue x = new Node_Queue();
        x.item = item;
        if (isEmpty()) { first = x;     last = x; }
        else           { last.next = x; last = x; }
        N++;
    }

    public Node dequeue() {
        if (isEmpty()) throw new RuntimeException("Queue underflow");
        Node item = first.item;
        first = first.next;
        N--;
        return item;
    }
    
    public String toString() {
        String s = "";
        for (Node_Queue x = first; x != null; x = x.next)
            s += x.item + " ";
        return s;
    }

    public Iterator<Node> iterator()  { return new QueueIterator();  }

    private class QueueIterator implements Iterator<Node> {
        private Node_Queue current = first;

        public boolean hasNext()  { return current != null; }
        public void remove() { throw new UnsupportedOperationException(); }

        public Node next() {
            if (!hasNext()) throw new NoSuchElementException();
            Node item = current.item;
            current = current.next; 
            return item;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -