數據結構之隊列

一、隊列ADT規格:

package com.java.framework.data_structure.queue;

/**
 * 隊列的ADT規格
 * Created by Ryan Xu on 2016/5/5.
 */
public interface Queue {

    /**
     * Make this queue empty
     * @author Ryan Xu
     */
    public void clear();

    /**
     * Add obj as rear element of this queue
     * @author Ryan Xu
     * @param obj
     */
    public void enqueue(Object obj);

    /**
     * Remove and return the front element of this queue
     * @author Ryan Xu
     * @return Object
     */
    public Object dedueue();


    /**
     * Return true if and only if this queue is empty
     * @author Ryan Xu
     * @return
     */
    public boolean isEmpty();


    /**
     * Return this queue's length
     * @author Ryan Xu
     * @return
     */
    public int size();


    /**
     * Return the element at the front of this queue -> 返回隊首元素
     * @author Ryan Xu
     * @return
     */
    public Object peek();

}


二、隊列的鏈式實現:

package com.java.framework.data_structure.queue;

/**
 * 用鏈表實現隊列
 * @author xhc
 */
public class LinkedQueue implements Queue {
	private SLLNode front, rear;
	private int count;

	//Construtor
	public LinkedQueue(){
		clear();
	}

	/**
	 * Inner Class
	 * 表示一個節點
	 * @author xhc
	 */
	private static class SLLNode{
		Object element;
		SLLNode next;

		//Construtor
		SLLNode(){

		}
		SLLNode(Object element){
			this.element = element;
		}
		SLLNode(Object element, SLLNode pNode){
			this.element = element;
			this.next = pNode;
		}
	}

	@Override
	public void clear() {
		front = rear = null;
		count = 0;
	}

	@Override
	public void enqueue(Object obj) {//入隊
		SLLNode pNode = new SLLNode(obj,null);
		if(rear == null){
			front = pNode;
		}else{
			rear.next = pNode;
		}
		rear = pNode;
		count++;
	}

	@Override
	public Object dedueue() {
		if(count ==0 ){
			throw new IllegalStateException();
		}
		Object val = front.element;
		front = front.next;
		if(front == null){
			rear =null;
		}
		count--;
		return val;
	}

	@Override
	public boolean isEmpty() {
		return count == 0;
	}

	@Override
	public int size() {
		return count;
	}

	@Override
	public Object peek() {
		if(isEmpty())
			throw new IllegalStateException();
		return front.element;
	}

	public String toString(){
		String buf = new String("[");
		SLLNode pNode = front;
		while(pNode != null){
			if(pNode != front){
				buf+=",";
			}
			buf+=pNode.element.toString();
			pNode = pNode.next;
		}
		buf+="]";
		return buf;
	}

}


三、通過數組來實現:

package com.java.framework.data_structure.queue;


/**
 * 用循環數組實現隊列
 * 隊列:先進先出(first-in-first-out)
 * @author xuhuanchao
 */
public class ArrayQueue implements Queue {

	private static final int DEFAULT_SIZE=5;//隊列默認長度爲5
	private Object[] array;
	private int front;
	private int rear;
	private int count;

	//Constructor
	public ArrayQueue(){
		array = new Object[DEFAULT_SIZE];
		front = rear = 0;
		count =0;
	}

	@Override
	public void clear() {//清空隊列
		for(int i=0; i<array.length; i++){
			array[i] = null;
			front = rear = count = 0;
		}
	}

	@Override
	public void enqueue(Object obj) {        //入隊
		if (count == array.length) {
			expand();
		}
		array[rear++] = obj;
		if (rear == array.length) {
			rear = 0;
		}
		count++;

	}

	@Override
	public Object dedueue() {
		if(isEmpty())
			throw new IllegalStateException();
		Object val = array[front];
		array[front++] = null;
		if(front == array.length){
			front = 0;
		}
		count--;
		return val;
	}


	@Override
	public boolean isEmpty() {
		return count == 0;
	}

	@Override
	public int size() {
		return count;
	}

	@Override
	public Object peek() {
		if(isEmpty()){
			throw new IllegalStateException();
		}
		return array[front];
	}

	/**
	 * 如果數組長度不夠,則成倍增加其長度
	 * @author xhc
	 */
	public void expand(){
		Object[] newArray = new Object[2 * array.length];
		for(int i=0; i<array.length; i++){
			newArray[i] = array[(front+i) % array.length];
		}
		front =0;
		rear = array.length;
		array = newArray;
	}

	/**
	 * 將隊列中的所有元素以逗號分割,然後以字符串形式返回
	 * @author xhc
	 */
	public String toString(){
		String buf = new String("[");
		for(int i=0; i<count; i++){
			if(i>0){
				buf+=",";
			}
			buf+=array[(front+i) % array.length].toString();
		}
		buf+="]";
		return buf;
	}

}

發佈了46 篇原創文章 · 獲贊 30 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章