数据结构之队列

一、队列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;
	}

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值