使用带有头尾节点的单链表实现队列

package db;

public class Queue<E> {
	/**
	 * 内部类--单链表
	 * @author fightzhao
	 *
	 * @param <E>
	 */
	private static class Node<E> {
		public Node<E> next;
		public E data;

		public Node(E data, Node<E> next) {
			this.data = data;
			this.next = next;
		}
	}

	private Node<E> front;
	private Node<E> rear;
	private int theSize;

	/**
	 * 入队
	 * @param data
	 */
	public void enqueue(E data) {
		Node<E> pNode = new Node<E>(data, null);
		if (rear != null) {
			rear.next = pNode;
			rear = pNode;
		} else {
			front = rear = pNode;
		}
		theSize++;
	}

	public int size() {
		return theSize;
	}

	/**
	 * 出队
	 * @return
	 */
	public E dequeue() {
		E data = front.data;
		if (front.next != null) {
			front = front.next;
		} else {
			front = rear = null;
		}
		theSize--;
		return data;
	}
	
	public Queue(){
		front = null;
		rear = null;
	}
	public void printAll(){
		Node<E> pNode= front;
		while(pNode!=null){
			System.out.println(pNode.data);
			pNode=pNode.next;
		}
	}
}

  

原文地址:https://www.cnblogs.com/fightzhao/p/5243247.html