day18 循环队列
1. 循环队列特点
-
用图去理解代码,循环队列我觉得一定要结合图去理解是容易的。入队时判断队是否已满再入队(顺序:先入值再修改指针);出队时判断队是否为空再出队(顺序:先出值再修改指针)
-
队空 对满的判定条件 (1)队满:(rear + 1) % MAX_SIZE = front;
(2)队空:front = rear 这里的队满判断是牺牲了一个空间,还有其他方式也是可以判断队满的。
2. 取模
我们可以从链队列可以看出,出队头指针加1,入队尾指针加1,头/尾指针不做任何处理,则会一直无限制向上加,会导致空间浪费。通过取模运行,就可以避免上述的问题,循环队列最大长度假设只有MAX_SIZE=10,取模则可以使头/尾指针做循环,进而空间最大化的使用。
3.代码
package datastructure.queue;
public class CircleIntQueue {
/**
* The total space. One space can never be used.
*/
public static final int TOTAL_SPACE = 10;
int[] data;
/**
* The index for calculating the head. The actual head is head % TOTAL_SPACE.
*/
int head;
/**
* The index for calculating the tail.
*/
int tail;
public CircleIntQueue(){
data = new int[TOTAL_SPACE];
head = 0;
tail = 0;
}
/**
* enqueue
* @param paraValue The value of the new node.
*/
public void enqueue(int paraValue){
if ((tail+1)%TOTAL_SPACE == head){
System.out.println("Queue full.");
return;
}
data[tail%TOTAL_SPACE] = paraValue;
tail++;
}
public int dequeue(){
if (head == tail){
System.out.println("No element in the queue");
return -1;
}
int resultValue = data[head%TOTAL_SPACE];
head++;
return resultValue;
}
@Override
public String toString(){
String resultString = "";
if (head == tail){
return "empty";
}
for (int i = head; i < tail; i++){
resultString += data[i%TOTAL_SPACE] + ", ";
}
return resultString;
}
public static void main(String[] args) {
CircleIntQueue tempQueue = new CircleIntQueue();
System.out.println("Initialized, the list is: " + tempQueue.toString());
for (int i = 0; i < 5; i++) {
tempQueue.enqueue(i + 1);
}
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
int tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
for (int i = 0; i < 6; i++) {
tempQueue.enqueue(i + 10);
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
}
for (int i = 0; i < 3; i++) {
tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
}
for (int i = 0; i < 6; i++) {
tempQueue.enqueue(i + 100);
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
}
}
}
字符类型:
package datastructure.queue;
public class CircleCharQueue {
public static final int TOTAL_SPACE = 10;
char[] data;
int head;
int tail;
/**
* The constructor
*/
public CircleCharQueue() {
data = new char[TOTAL_SPACE];
head = 0;
tail = 0;
}
/**
* Enqueue.
* @param paraValue The value of the new node.
*/
public void enqueue(char paraValue) {
if ((tail + 1) % TOTAL_SPACE == head) {
System.out.println("Queue full.");
return;
}
data[tail % TOTAL_SPACE] = paraValue;
tail++;
}
/**
* Dequeue
* @return The value at the head.
*/
public char dequeue() {
if (head == tail) {
System.out.println("No element in the queue");
return '\0';
}
char resultValue = data[head % TOTAL_SPACE];
head++;
return resultValue;
}
/**
* Overrides the method claimed in Object, the superclass of any class.
* @return
*/
@Override
public String toString() {
String resultString = "";
if (head == tail) {
return "empty";
}
for (int i = head; i < tail; i++) {
resultString += data[i % TOTAL_SPACE] + ", ";
}
return resultString;
}
public static void main(String args[]) {
CircleCharQueue tempQueue = new CircleCharQueue();
System.out.println("Initialized, the list is: " + tempQueue.toString());
for (char i = '0'; i < '5'; i++) {
tempQueue.enqueue(i);
}
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
char tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
for (char i = 'a'; i < 'f'; i++) {
tempQueue.enqueue(i);
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
}
for (int i = 0; i < 3; i++) {
tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
}
for (char i = 'A'; i < 'F'; i++) {
tempQueue.enqueue(i);
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
}
}
}