日撸Java三百行 day17(链队列)

1. 队列及基本操作

1.1 队列

队列是一种操作受限的线性表,它只能在一边插入(入队),另一边删除(出队)。基于不同存储方式分为顺序队列和链式队列,本篇主要讲链式队列。

链队列的初始化:

Node header;
Node tail;	
public LinkedQueue() {
	header = new Node(-1);
	//header.next = null;
		
    tail = header;
}// Of the first constructor

当tail = header 时,此时队空,可做出队时的判空条件。 

1.2 入队

链式队列由于链式的特性,一般没有队满的说法,所以在入队前不需要做判满工作。入队时先建立新的节点,然后让tail的next指向新节点,然后再更新tail让他指向新节点。

public void enqueue(int paraValue) {
	Node tempNode = new Node(paraValue);
	tail.next = tempNode;
	tail = tempNode;
}// Of enqueue

1.3 出队

链式队列在出队前后都需做判空测试。出队前若队列中还存在元素则可以进行出队操作,否则直接返回-1。出队时将出队元素存放在临时变量中,然后让header的next指向出队元素的后一个,返回出队元素。出队后判断header后是否还有元素,如没有则需要把tail = header,以便后续的判空操作。

public int dequeue() {
	if (tail == header) {
		System.out.println("No element in the queue.");
		return -1;
	} // Of if
		
	int resultValue = header.next.data;
		
	header.next =header.next.next;
		
	// The queue beacomes empty.
	if (header.next == null) {
		tail = header;
	} // Of if
		
	return resultValue;
}// Of dequeue

2.链队列的简单测试

package datastructure.queue;

/**
 * Linked queue.
 * 
 * @author Yunhua Hu yunhuahu0528@163.com.
 */

public class LinkedQueue {
	
	/**
	 * An inner queue.
	 */
	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.
	 *********************
	 */
	public 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 (tail == header) {
			System.out.println("No element in the queue.");
			return -1;
		} // Of if
		
		int resultValue = header.next.data;
		
		header.next =header.next.next;
		
		// The queue beacomes empty.
		if (header.next == null) {
			tail = header;
		} // Of if
		
		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 uesd now.
	 *********************
	 */
	public static void main(String args[]) {
		LinkedQueue tempQueue = new LinkedQueue();
		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
		
		for (int i = 0; i < 3; i++) {
			tempQueue.enqueue(i + 10);
		} // Of for i
		System.out.println("Enqueue, the queue is: " + tempQueue.toString());
	}// Of main
}// Of calss LinkedQueue

输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值