Queue
队列是一个先入先出(FIFO)的数据结构
Java 集合中的 Queue 继承自 Collection 接口 ,Deque, LinkedList, PriorityQueue, BlockingQueue 等类都实现了它
方法
jdk1.5中的阻塞队列的方法:
add 增加一个元素
如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove 移除并返回队列头部的元素
如果队列为空,则抛出一个NoSuchElementException异常
element 返回队列头部的元素
如果队列为空,则抛出一个NoSuchElementException异常
offer 添加一个元素并返回true
如果队列已满,则返回false poll
移除并返问队列头部的元素 如果队列为空,则返回null
peek 返回队列头部的元素
如果队列为空,则返回null
put添加一个元素
如果队列满,则阻塞
take 移除并返回队列头部的元素
如果队列为空,则阻塞
add/offer,element/peek,remove/poll区别
1、add()和offer()区别:
add()和offer()都是向队列中添加一个元素。一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,调用 add() 方法就会抛出一个 unchecked 异常,而调用 offer() 方法会返回 false。因此就可以在程序中进行有效的判断!
2、poll()和remove()区别:
remove() 和 poll() 方法都是从队列中删除第一个元素。如果队列元素为空,调用remove() 的行为与 Collection 接口的版本相似会抛出异常,但是新的 poll() 方法在用空集合调用时只是返回 null。因此新的方法更适合容易出现异常条件的情况。
3、element() 和 peek() 区别:
element() 和 peek() 用于在队列的头部查询元素。与 remove() 方法类似,在队列为空时, element() 抛出一个异常,而 peek() 返回 null。
手写单向队列/环形队列
单向队列
class ArrayQueue{
private int maxSize;
private int front;
private int rear;
private int[] arr;
//创建队列构造器
public ArrayQueue(int arrMaxSize){
maxSize = arrMaxSize;
front = -1;
rear = -1;
arr = new int[arrMaxSize];
}
//判断队列是否满
public boolean isFull(){
return rear == maxSize-1;
}
//判断队列是否空
public boolean isEmpty(){
return rear == front;
}
//添加数据到队列
public void addQueue(int n){
if(isFull()){
System.out.println("the queue is full!!!!");
return;
}
arr[++rear] = n;
}
public int getQueue(){
if(isEmpty()){
throw new RuntimeException("the queue is empty!!!!");
}
front++;
return arr[front];
}
public void showQueue(){
if(isEmpty()){
throw new RuntimeException("the queue is empty!!!!");
}
for(int i = 0;i<arr.length;i++){
System.out.printf("arr[%d]=%d\n",i,arr[i]);
}
}
public int HeadQueue(){
if (isEmpty()){
throw new RuntimeException("the queue is empty!!!!");
}
return arr[front+1];
}
}
环形队列
class CircleQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
//创建队列构造器
public CircleQueue(int arrMaxSize){
maxSize = arrMaxSize;
front = 0;
rear = 0;
arr = new int[arrMaxSize];
}
//判断队列是否满
public boolean isFull(){
return (rear + 1) % maxSize == front;
}
//判断队列是否空
public boolean isEmpty(){
return rear == front;
}
//添加数据到队列
public void addQueue(int n){
if(isFull()){
System.out.println("the queue is full!!!!");
return;
}
arr[rear] = n;
rear = (rear + 1) %maxSize;
}
public int getQueue(){
if(isEmpty()){
throw new RuntimeException("the queue is empty!!!!");
}
int value = arr[front];
front = (front + 1 ) % maxSize;
return value;
}
public void showQueue(){
if(isEmpty()){
throw new RuntimeException("the queue is empty!!!!");
}
for(int i = front;i<front + getNumber();i++){
System.out.printf("arr[%d]=%d\n",i % maxSize,arr[i % maxSize]);
}
}
public int HeadQueue(){
if (isEmpty()){
throw new RuntimeException("the queue is empty!!!!");
}
return arr[front];
}
public int getNumber(){
if (isEmpty()){
throw new RuntimeException("the queue is empty!!!!");
}
return (rear + maxSize -front)%maxSize;
}
}
lc.649 Dota2 参议院
用两个队列分别保存天辉和夜魇议员的发言顺序,不断检查两个队列头部元素的先后关系
class Solution {
public String predictPartyVictory(String senate) {
Queue<Integer> qR = new LinkedList<>();
Queue<Integer> qD = new LinkedList<>();
for(int i=0;i<senate.length();i++){
if (senate.charAt(i) == 'R')
qR.add(i);
else
qD.add(i);
}
while(!qR.isEmpty() && !qD.isEmpty()){
if(qR.peek()<qD.peek()){
qR.add(qR.poll() + senate.length());
qD.poll();
}else{
qD.add(qD.poll() + senate.length());
qR.poll();
}
}
return qR.isEmpty() ? "Dire" : "Radiant";
}
}