链表 数组 向量实现的队列(范型)

公共接口

package edu.cumt.jnotnull;

public interface QueueInterface<T> {
	public void enqueue(T newEntry);
	public T dequeue();
	public T getFront();
	public boolean isEmpty();
	public void clear();	
}

 链表

package edu.cumt.jnotnull;

import java.io.Serializable;

public class LinkedQueue<T> implements Serializable, QueueInterface<T> {
	private Node<T> firstNode;
	private Node<T> lastNode;

	public LinkedQueue() {
		firstNode = null;
		lastNode = null;
	}

	@Override
	public void clear() {
		firstNode = null;
		lastNode = null;
	}

	@Override
	public T dequeue() {
		T front = null;
		if(!isEmpty()){
			front = firstNode.getData();
			firstNode = firstNode.getNext();
			if(firstNode == null){
				lastNode = null;
			}
		}
		return front;
	}

	@Override
	public void enqueue(T newEntry) {
		Node newNode = new Node(newEntry, null);
		if (isEmpty()) {
			firstNode = newNode;
			lastNode = newNode;
		} else {
			lastNode.setNext(newNode);
		}
	}

	@Override
	public T getFront() {
		T front = null;
		if(!isEmpty()){
			front = firstNode.getData();
		}
		return front;
	}

	@Override
	public boolean isEmpty() {
		return firstNode == null;
	}

	private class Node<T> implements Serializable {
		private T data;
		private Node<T> next;

		private Node(T dataPortion) {
			data = dataPortion;
			next = null;
		}

		private Node(T dataPortion, Node<T> nextNode) {
			data = dataPortion;
			next = nextNode;
		}

		public T getData() {
			return data;
		}

		public void setData(T data) {
			this.data = data;
		}

		public Node<T> getNext() {
			return next;
		}

		public void setNext(Node<T> next) {
			this.next = next;
		}

	}
}

 

数组(删除前端时,可以让其它数组元素仍留在现在位置上,同时一旦增加元素到达数组末尾,可以将后续的元素插入到数组的头部,backIndex = (backIndex+1)%queue.length;另外 为了区分空数组和满数组,使用一个未用位置的循环数组,满队列时frontIndex == (backIndex+2)%queue.length; 空队列时frontIndex == (backIndex+1)%queue.length)

package edu.cumt.jnotnull;

import java.io.Serializable;

public class ArrayQueue<T> implements QueueInterface<T>, Serializable {
	private T[] queue;
	private int frontIndex;
	private int backIndex;
	private static final int DEFAULT_INITIAL_CAPACITY = 50;
	
	public ArrayQueue(){
		this(DEFAULT_INITIAL_CAPACITY);
	}
	
	public ArrayQueue(int initialCapacity){
		queue = (T[])new Object[initialCapacity+1];
		frontIndex = 0;
		backIndex = initialCapacity;
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public T dequeue() {
		T front = null;
		if(!isEmpty()){
			front = queue[frontIndex];
			queue[frontIndex] = null;
			frontIndex = (frontIndex+1)%queue.length;
		}
		return front;
	}

	@Override
	public void enqueue(T newEntry) {
		if(isArrayFull()){
			doubleArray();
		}
		backIndex = (backIndex+1)%queue.length;
		queue[backIndex] = newEntry;
	}

	@Override
	public T getFront() {
		T front = null;
		if(!isEmpty()){
			front = queue[frontIndex];
		}
		return front;
	}

	@Override
	public boolean isEmpty() {
		return frontIndex == (backIndex+1)%queue.length;
	}
	
	private boolean isArrayFull(){
		return frontIndex == (backIndex+2)%queue.length;
	}
	
	private void doubleArray(){
		T [] oldQueue = queue;
		int oldSize = oldQueue.length;
		queue = (T[])new Object[2*oldSize];
		for(int index = 0;index<oldSize-1;index++){
			queue[index] = oldQueue[frontIndex];
		}
		frontIndex = 0;
		backIndex = oldSize-2;
	}

}

 

向量

package edu.cumt.jnotnull;

import java.io.Serializable;
import java.util.Vector;

public class VectorQueue<T> implements QueueInterface<T>, Serializable {
	private Vector<T> queue;
	
	
	public VectorQueue() {
		this.queue = new Vector<T>();
	}
	
	public VectorQueue(int initialCapacity) {
		this.queue = new Vector<T>(initialCapacity);
	}

	@Override
	public void clear() {
		queue.clear();
	}

	@Override
	public T dequeue() {
		T front = null;
		if(!isEmpty()){
			front = queue.remove(0);
		}
		return front;
	}

	@Override
	public void enqueue(T newEntry) {
		queue.addElement(newEntry);
	}

	@Override
	public T getFront() {
		T front = null;
		if(!isEmpty()){
			front = queue.get(0);
		}
		return front;
	}

	@Override
	public boolean isEmpty() {		
		return queue.isEmpty();
	}

}

 

你可能感兴趣的:(数组)