數據結構
一群數據集合和數據之間的關系。
是指相互之間存在一種或多種特定關系的數據元素集合。
隊列
特點:先入先出(First in First out ---FIFO)
火車站買票。隊頭隊尾。售票員從頭開始賣票。先到的人先買到票就可以離開。
普通隊列
普通隊列.png
普通隊列有限制:
1.售票員走,浪費隊列前面的空間。
2.隊伍走,需要移動數組,速度慢。
環形隊列
環形隊列.png
能大大優化普通隊列
有隊列頭,有隊列尾。只有一個元素,既是隊列頭,又是隊列尾。
用途:自動排號機
環形隊列代碼
package ss;
/**
* 環形隊列Demo
* 簡單環形隊列的構建
*
* MyQueue(int capacity) 構造方法(隊列大小)
* clearQueue() 清空隊列
* destoryQueue() 銷毀隊列對象
* isEmpty() 隊列判空
* isFull() 隊列判滿
* getLength() 獲取隊列長度
* EnQueue(T) 入隊
* DeQueue() 出隊,返回隊列的元素
* traverseQueue() 遍歷隊列
* doSomething(T t) 具體實現邏輯,讓用戶具體實現
*
* @author Administrator
* @date 2017年10月24日 下午4:58:29
* @param <T> 隊列元素
* 因為存放進隊列的元素我們是未知的
* 但是可以確定的是一個隊列里面的元素基本是固定的
* 所以這里使用了泛型,讓用戶真正使用的時候才去確定
*/
public abstract class MyQueue<T extends Object> {
//隊列對象
//使用Object數組和泛型是為了讓隊列可以兼容各種類型的對象;
private Object[] mQueue;
//隊列當前長度
private int mLength;
//隊列容量
private int mCapacity;
//隊頭,出隊時出隊的位置
private int mHead;
//對尾,入隊時入隊的位置
private int mEnd;
/**
* 構造方法
* @param capacity 隊列的容量大小
*/
public MyQueue(int capacity) {
mCapacity =capacity;
//初始化參數
mQueue=new Object[mCapacity];
clearQueue();
}
/**
* 清空隊列內的參數
*@date 2017年10月24日 下午5:05:19
*/
private void clearQueue() {
mLength=0;
mHead=0;
mEnd=0;
}
/**
* 銷毀隊列
* 及時釋放資源
*@date 2017年10月24日 下午5:08:11
*/
public void destoryQueue(){
clearQueue();
mQueue=null;
System.gc();
}
/**
* 判斷當前隊列是否為空
* 空:true 非空:flase
*@date 2017年10月24日 下午5:10:12
*/
public boolean isEmpty(){
return mLength==0;
}
/**
* 判斷當前隊列是否已滿
*@date 2017年10月24日 下午5:11:58
*/
public boolean isFull(){
return mLength==mCapacity;
}
/**
* 獲取當前隊列長度
*@date 2017年10月24日 下午5:20:27
*/
public int getLength(){
return mLength;
}
/**
* 添加元素到隊列當中(入隊)
* 從隊尾開始入隊
* @param t 需要入隊的對象
* @return 入隊是否成功
*@date 2017年10月24日 下午5:24:12
*/
public boolean EnQueue(T t){
if(isFull()){
return false;
}
mQueue[mEnd] =t;
mEnd++;
mLength++;
/**
* 因為是環形隊列模型,所以當隊頭出隊以后
* 就會空出位置,隊尾自然就可以往空位置上移動
* 所以這里使用%來出來循環
*/
mEnd=mEnd%mCapacity;
return true;
}
/**
* 返回隊頭位置的元素對象(出隊)
*@date 2017年10月24日 下午5:27:52
*/
public T DeQueue(){
if(isEmpty()){
return null;
}
T t =(T) mQueue[mHead];
mHead++;
mLength--;
//這里使用的原理和出隊是一樣的可以看一下上面:)
mHead = mHead%mCapacity;
return t;
}
/**
* 遍歷當前隊列全部對象
*@date 2017年10月24日 下午5:30:25
*/
public void traverseQueue(){
for (int i = mHead; i < mHead+mLength; i++) {
doSomething((T)mQueue[i%mCapacity]);
}
}
/**
* 具體處理隊列對象的方法
* 這里使用抽象方法因為:
* 對于對象的具體處理邏輯各不相同,所以具體實現交給用戶自己去完成
*@date 2017年10月24日 下午5:30:41
*/
public abstract void doSomething(T t);
}
測試代碼
package ss;
public class TestDemo {
public static void main(String[] args) {
MyQueue<A> a=new MyQueue<A>(5){
@Override
public void doSomething(A t) {
System.out.println(t.toString());
}
};
A a1=new A(1,"a1");
A a2=new A(2,"a2");
A a3=new A(3,"a3");
A a4=new A(4,"a4");
A a5=new A(5,"a5");
A a6=new A(6,"a6");
a.EnQueue(a1);
a.EnQueue(a2);
a.EnQueue(a3);
a.EnQueue(a4);
a.EnQueue(a5);
a.EnQueue(a6);
a.traverseQueue();
System.out.println("-----------------------------------------------------");
a.DeQueue();
a.DeQueue();
a.EnQueue(a6);
a.traverseQueue();
}
}
因為慕課網的視頻是用c語言寫的,我在簡書上找到了java寫的,代碼來自Rayhaha附上鏈接http://t.cn/RWCfxtF