用java实现链式队列
时间: 2023-04-07 08:01:23 浏览: 91
好的,我可以回答这个问题。链式队列是一种基于链表实现的队列,它可以动态地添加和删除元素。在Java中,可以通过定义一个Node类来实现链表节点,再定义一个Queue类来实现队列的基本操作,如入队、出队、判断队列是否为空等。具体实现可以参考以下代码:
```
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class Queue {
Node front, rear;
public Queue() {
this.front = this.rear = null;
}
public void enqueue(int data) {
Node newNode = new Node(data);
if (this.rear == null) {
this.front = this.rear = newNode;
return;
}
this.rear.next = newNode;
this.rear = newNode;
}
public void dequeue() {
if (this.front == null) {
return;
}
Node temp = this.front;
this.front = this.front.next;
if (this.front == null) {
this.rear = null;
}
}
public boolean isEmpty() {
return this.front == null;
}
}
```
以上就是用Java实现链式队列的基本代码。