JAVA学习(小白向)—链队列—2021-06-05

这篇博客介绍了链队列的概念,它是一种使用链式存储结构的队列。作者通过一个简单的JAVA类展示了如何创建和操作链队列,包括入队(enqueue)和出队(dequeue)操作,并提供了完整的代码示例。示例代码中,链队列的节点由内部类定义,同时包含头节点和尾节点,实现了队列的基本功能。在测试代码中,展示了队列的初始化、元素添加、删除以及打印队列内容的过程。

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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值