在TestQueue类里面创建一个链队列,把数字0-9按顺序入队列,并对其进行遍历,输出遍历结果
时间: 2024-05-10 08:20:40 浏览: 28
```java
public class TestQueue {
public static void main(String[] args) {
LinkedQueue<Integer> queue = new LinkedQueue<>();
for (int i = 0; i < 10; i++) {
queue.enqueue(i);
}
while (!queue.isEmpty()) {
System.out.print(queue.dequeue() + " ");
}
}
}
```
输出结果:
```
0 1 2 3 4 5 6 7 8 9
```
相关问题
在TestQueue类里面创建一个链队列,将数字0-9按顺序入队列,并对其进行遍历,输出遍历结果
链队列的实现需要先定义一个节点类,然后在队列类中使用节点类来定义队列节点。以下是一个简单的链队列的实现:
```python
class QueueNode:
def __init__(self, value):
self.value = value
self.next = None
class LinkedQueue:
def __init__(self):
self.head = None
self.tail = None
def is_empty(self):
return self.head is None
def enqueue(self, value):
node = QueueNode(value)
if self.is_empty():
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def dequeue(self):
if self.is_empty():
raise Exception("Queue is empty")
node = self.head
self.head = node.next
if self.head is None:
self.tail = None
return node.value
def __iter__(self):
node = self.head
while node is not None:
yield node.value
node = node.next
```
然后在TestQueue类中使用该链队列类来创建队列,入队0-9这10个数字,并遍历输出结果:
```python
queue = LinkedQueue()
for i in range(10):
queue.enqueue(i)
for value in queue:
print(value)
```
输出结果为:
```
0
1
2
3
4
5
6
7
8
9
```
在TestQueue类里面创建一个链队列,将数字0-9按顺序入队列,并对其进行遍历,输出遍历结果。
代码如下:
```java
public class TestQueue {
public static void main(String[] args) {
LinkedQueue<Integer> queue = new LinkedQueue<>();
for (int i = 0; i < 10; i++) {
queue.enqueue(i);
}
System.out.print("遍历结果:");
while (!queue.isEmpty()) {
System.out.print(queue.dequeue() + " ");
}
}
}
```
其中,`LinkedQueue`是链队列的实现类,代码如下:
```java
public class LinkedQueue<T> {
private Node<T> front; // 队头指针
private Node<T> rear; // 队尾指针
private static class Node<T> {
T data; // 数据域
Node<T> next; // 指针域
public Node(T data) {
this.data = data;
}
}
public LinkedQueue() {
front = rear = null;
}
// 入队
public void enqueue(T data) {
Node<T> node = new Node<>(data);
if (isEmpty()) {
front = rear = node;
} else {
rear.next = node;
rear = node;
}
}
// 出队
public T dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty!");
}
T data = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return data;
}
// 队列是否为空
public boolean isEmpty() {
return front == null;
}
}
```
运行结果:
```
遍历结果:0 1 2 3 4 5 6 7 8 9
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)