手写实现一个队列java
时间: 2024-06-01 20:01:53 浏览: 30
public class Queue<T> {
private Node<T> head;
private Node<T> tail;
public void enqueue(T value) {
Node<T> newNode = new Node<>(value);
if (tail == null) {
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
}
public T dequeue() {
if (head == null) {
return null;
}
T value = head.value;
head = head.next;
if (head == null) {
tail = null;
}
return value;
}
public boolean isEmpty() {
return head == null;
}
private static class Node<T> {
T value;
Node<T> next;
Node(T value) {
this.value = value;
}
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)