用陣列實現環形佇列

2021-10-02 11:31:41 字數 693 閱讀 5408

class circlearray

//判斷佇列滿

//boolean型,返回 ture 或 false

public boolean isfull()

//判斷佇列空

public boolean isempty()

//寫資料,僅限 int 型

public void addqueue(int n)

arr[rear] = n; //寫資料入佇列

rear = (rear + 1) % maxsize; //尾指標後退一位, 取模是必須考慮的,因為環形

}//取資料

public int getfront()

int value = arr[front]; //先將資料存入乙個臨時變數,等會直接返回這個數值

front = (front + 1) % maxsize;

return value;

}//顯示所有的資料,遍歷

public void showqueue()

for (int i = front; i < front + size(); i++)

}public int size()

public int headqueue()

return arr[front];

}}

誰都能看懂的用陣列實現環形佇列

介紹 佇列又稱為 先進先出 fifo 線性表。限定插入操作只能在隊尾進行,而刪除操作只能在隊首進行 應用場景 排隊front 佇列頭,指向佇列的第乙個元素,初始值為0 rear 佇列尾,只想佇列的最後乙個元素的後乙個位置,所占用的位置作為約定 空乙個位置 初始值為0 maxsize 總長度初始值需建...

陣列模擬環形佇列

class queue 判斷佇列是否滿 public boolean isfull 判斷佇列是否為空 public boolean isempty 新增資料 param n 新增資料的值 public void addqueue int n arr rear n 注意 這裡一定要 去摸 而且 注意 ...

陣列模擬環形佇列

package queue 陣列模擬環形佇列 public class queue public void add int element else public intremove throws exception int temp arr front front front 1 size ret...