📄 myqueue.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 + -