链式队列的实现

package Queue;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;

public class LinkedQueue<T> {

	private LinkedList<T> qList = null;

	private int qSize = 0;

	public LinkedQueue() {
		qList = new LinkedList<T>();
	}

	/**如果队列为空,返回true, 否则返回false*/
	public boolean isEmpty() {
		return qList.isEmpty();
	}

	/**返回队列元素个数*/
	public int size() {
		return qSize;
	}
	
	/**在队列的尾部插入指定元素*/
	public void push(T item) {
		qList.add(item);
		qSize++;
	}

	/**删除队列头部的元素并且返回这个元素*/
	public T pop() {
		T tmp;
		if (this.isEmpty())
			throw new NoSuchElementException(
					"LinkedQueue pop(): queue empty");
		tmp = qList.removeFirst();
		qSize--;
		return tmp;
	}
	
	/**返回位于队列头部的元素*/
	public T peek() {
		if (this.isEmpty())
			throw new NoSuchElementException(
					"LinkedQueue peek(): queue empty");
		return qList.getFirst();
	}
	
	/**打印队列内元素*/
	public void print() {
		T item;
		Iterator<T> it = qList.iterator();
		while(it.hasNext()) {
			item = it.next();
			System.out.print(item + " ");
		}
	}

	/*public static void main(String[] args) {
		LinkedQueue<String> q = new LinkedQueue<String>();
		q.push("A");
		q.push("B");
		q.push("C");
		System.out.println(q.size()); // 3
		q.print(); // A B C
		System.out.println('\n' + q.pop()); // A
		q.print(); // B C
		System.out.println('\n' + q.peek()); // B
	}*/

}

你可能感兴趣的:(java,C++,c,C#)