auto commit
This commit is contained in:
parent
d2e33dd46b
commit
775902ade6
46
notes/算法.md
46
notes/算法.md
|
@ -446,52 +446,6 @@ public class ListQueue<Item> implements MyQueue<Item> {
|
|||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class Queue<Item> {
|
||||
private Node first;
|
||||
private Node last;
|
||||
int N = 0;
|
||||
|
||||
private class Node {
|
||||
Item item;
|
||||
Node next;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return N == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return N;
|
||||
}
|
||||
|
||||
public void add(Item item) {
|
||||
Node newNode = new Node();
|
||||
newNode.item = item;
|
||||
newNode.next = null;
|
||||
if (isEmpty()) {
|
||||
last = newNode;
|
||||
first = newNode;
|
||||
} else {
|
||||
last.next = newNode;
|
||||
last = newNode;
|
||||
}
|
||||
N++;
|
||||
}
|
||||
|
||||
public Item remove() throws Exception {
|
||||
if (isEmpty())
|
||||
throw new Exception("queue is empty");
|
||||
Node node = first;
|
||||
first = first.next;
|
||||
N--;
|
||||
if (isEmpty())
|
||||
last = null;
|
||||
return node.item;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 三、排序
|
||||
|
||||
待排序的元素需要实现 Java 的 Comparable 接口,该接口有 compareTo() 方法,可以用它来判断两个元素的大小关系。
|
||||
|
|
Loading…
Reference in New Issue
Block a user