JAVA学习(小白向)—链队列—2021-06-05
Q&A
Q1:什么是链队列?
A1:链队是指采用链式存储结构实现的队列,通常链队用单链表俩表示。一个链队显然需要两个分别指示队头和队尾的指针,也称为头指针和尾指针,有了这两个指针才能唯一的确定;入队仅操作尾部, 出队仅操作头部.
Q2:链表和链队列的区别?
代码
package a15;
/**
* ******************** Linked list.
*
* @author hengyuzuo ********************
*/
public class Queue {
/**
* *************** An inner class. ***************
*/
class Node {
/**
* *********** The data. ***********
*/
int data;
/**
* *********** The reference to the next node. ***********
*/
Node next;
/****************
* The constructor.
*
* @param paraValue the data. **************
*/
public Node(int paraValue) {
data = paraValue;
next = null;
}// Of the constructor
}// Of class Node
/**
* ***************** The header of the queue. *****************
*/
Node header;
/**
* ******************* The tail of the queue. *******************
*/
Node tail;
/**
* ******************* Constructor an empty sequential list. *
*
* @return ******************
*/
public void LinkedQueue() {
header = new Node(-1);
header.next = null;
tail = header;
}// Of the first constructor
/**
* ******************** Enqueue.
*
* @param paraValue the value of the new node. ********************
*/
public void enqueue(int paraValue) {
Node tempNode = new Node(paraValue);
tail.next = tempNode;
tail = tempNode;
}// Of enqueue
/**
* ******************** Dequeue.
*
* @return The value at the header. ********************
*/
public int dequeue() {
if (header.next == null) {
System.out.println("No element in the queue");
return -1;
} // Of if
int resultValue = header.next.data;
header.next = header.next.next;
return resultValue;
}// Of dequeue
/**
* ***************************
* Overrides the method claimed in object, the superclass of any class.
* ***************************
*/
public String toString() {
String resultString = "";
if (header.next == null) {
return "empty";
}// Of if
Node tempNode = header.next;
while (tempNode != null) {
resultString += tempNode.data + ",";
tempNode = tempNode.next;
}// Of while
return resultString;
}// Of toString
/**
* **********************
* The entrance of the program
* @param args not used now.
* **********************
*/
public static void main(String args[]) {
Queue tempQueue = new Queue();
System.out.println("Initialized, the list is: " + tempQueue.toString());
for (int i = 0; i < 5; i++) {
tempQueue.enqueue(i + 1);
}// Of for i
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
tempQueue.dequeue();
System.out.println("Dequeue, the queue is: " + tempQueue.toString());
int tempValue;
for (int i = 0; i < 5; i++) {
tempValue = tempQueue.dequeue();
System.out.println("Looped delete " + tempValue + ", the new queue is: " + tempQueue.toString());
}// Of for i
}// Of for main
}// Of class Queue
运行结果
Initialized, the list is: empty
Enqueue, the queue is: 1, 2, 3, 4, 5,
Dequeue, the queue is: 2, 3, 4, 5,
Looped delete 2, the new queue is: 3, 4, 5,
Looped delete 3, the new queue is: 4, 5,
Looped delete 4, the new queue is: 5,
Looped delete 5, the new queue is: empty
No element in the queue
Looped delete -1, the new queue is: empty
这篇博客介绍了链队列的概念,它是一种使用链式存储结构的队列。作者通过一个简单的JAVA类展示了如何创建和操作链队列,包括入队(enqueue)和出队(dequeue)操作,并提供了完整的代码示例。示例代码中,链队列的节点由内部类定义,同时包含头节点和尾节点,实现了队列的基本功能。在测试代码中,展示了队列的初始化、元素添加、删除以及打印队列内容的过程。
2148

被折叠的 条评论
为什么被折叠?



