java利用循环双链表设计队列的代码怎么写
时间: 2023-05-31 19:07:28 浏览: 103
这是一个技术问题,我可以回答。以下是利用循环双链表设计队列的 Java 代码:
```
public class Queue {
private Node head, tail;
private int size;
public Queue() {
head = null;
tail = null;
size = 0;
}
public boolean isEmpty() {
return head == null;
}
public int getSize() {
return size;
}
public void enqueue(int data) {
if (tail == null) {
tail = new Node(data);
head = tail;
head.prev = tail;
tail.next = head;
} else {
Node newNode = new Node(data);
newNode.prev = tail;
newNode.next = head;
tail.next = newNode;
head.prev = newNode;
tail = newNode;
}
size++;
}
public int dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
int data = head.data;
head = head.next;
tail.next = head;
head.prev = tail;
size--;
return data;
}
private class Node {
int data;
Node prev, next;
public Node(int data) {
this.data = data;
prev = null;
next = null;
}
}
}
```
以上代码利用循环双链表实现了一个简单的队列。请注意,这只是一个示例,实际应用中需要根据具体需求修改。
阅读全文