資料結構與演算法--陣列模擬佇列(Queue)
阿新 • • 發佈:2021-01-23
此文章僅作為自己學習過程中的記錄和總結,同時會有意地去用英文來做筆記,一些術語的英譯不太準確,內容如有錯漏也請多指教,謝謝!
一、概述
需要強調的是,此文所指的佇列並非之後的環形佇列、迴圈佇列、鏈佇列等,就是最普通的用陣列模擬的順序佇列。
佇列的基本組成結構為:
- (int) maxSIze:佇列的最大容量。
- (int) front:指向佇列頭的“指標”。(實際上儲存的是指向佇列第一個元素的前一個位置的下標)
- (int) rear:指向佇列尾的“指標”。(區別於front,所儲存的就是佇列最後一個元素的位置下標)
- (E[ ]) queueArr:模擬佇列的陣列。(E的型別取決於實際情況)
(從上圖其實也可以看出普通陣列佇列的缺陷)
內容:
- 構造方法建立陣列佇列。
- isEmpty(), isFull()【判斷佇列是否為空/滿】
- addQueue()【向佇列中新增元素】
- getQueue()【獲取佇列頭元素並將front向後移一個,實現“假出佇列”的效果】
- peekQueue()【獲取佇列頭元素但不影響front的值】
- showQueue()【展示佇列所有元素】
二、程式碼實現
- Attributes and constructor
/*
ArrayQueue
Zzay
2021/01/14
*/
package com.zzay.queue;
/**
* TIPS:
* (1) "front" indicator points to the previous place of the first element,
* and it changes with the output of the queue.
* (2) "rear" indicator points to the last element, and it changes with the input of the queue.
*
* PROBLEMS:
* (1) This kind of queue can be used only once. It cannot provide the function of reusing.
*
* SOLUTION:
* (1) Using a certain algorithm to convert this queue into a circular queue.
*
* @author Zzay
* @version 2021/01/14
*/
public class ArrayQueue {
// The maximum capacity of the array.
private int maxSize;
// The indicator of the front element of the queue.
// The front indicator points to the previous place of the first elements.
private int front;
// The indicator of the rear element of the queue.
// The rear indicator includes the last element.
private int rear;
// The array that modifies a queue.
private int[] queueArr;
/**
* Receive a capacity and instantiate a queue array with the max size of the same value.
* Also, do initializations.
*
* @param capacity The expected max size of the queue array
*/
public ArrayQueue(int capacity) {
if (capacity <= 0) {
System.out.println("Invalid capacity, enter again...");
return;
}
maxSize = capacity;
queueArr = new int[maxSize];
initialize();
}
/**
* Initialize the front and rear indicator of the queueArr.
*/
private void initialize() {
front = -1;
rear = -1;
}
}
- Methods
/**
* Judge whether the queue is empty or not.
*
* @return True if the queue is empty; false if the queue is not empty
*/
public boolean isEmpty() {
if (front == rear) {
return true;
}
return false;
}
/**
* Judge whether the queue is full or not.
*
* @return True if the queue is full; false if the queue is not full
*/
public boolean isFull() {
if (rear == maxSize - 1) {
return true;
}
return false;
}
/**
* Add an element into the queue if it's not full.
*/
public void addQueue(int data) {
//Judge if the queue is full or not.
if (isFull()) {
System.out.println("The queue is full, cannot add new element...");
return;
}
rear++;
queueArr[rear] = data;
}
/**
* Get the first element from the queue if it's not empty.
*
* @return The first element in the queue
*/
public int getQueue() {
//Judge if the queue is empty or not.
if (isEmpty()) {
throw new RuntimeException("The queue is empty, cannot get element from it...");
}
front++;
return queueArr[front];
}
/**
* Get the data of the first element in the queue, without affecting its existence.
*/
public int peekQueue() {
if (isEmpty()) {
throw new RuntimeException("The queue is empty, cannot peek the first element...");
}
return queueArr[front + 1];
}
/**
* Display the data in the queue.
*/
public void showQueue() {
if (isEmpty()) {
System.out.println("The queue is empty...");
return;
}
int count = 0;
for (int i = front + 1; i < rear + 1; i++) {
System.out.printf("array[%d]: %d\n", count++, queueArr[i]);
}
System.out.println();
}
- Test
/*
ArrayQueue
Zzay
2021/01/14
*/
package com.zzay.queue;
import java.util.Scanner;
/**
* @author Zzay
* @version 2021/01/14
*/
public class ArrayQueueTest {
public static void main(String[] args) {
char key = ' ';
boolean loop = true;
Scanner scanner = new Scanner(System.in);
ArrayQueue arrayQueue = new ArrayQueue(3);
while (loop) {
System.out.println("s(show): show queue");
System.out.println("a(add): add element");
System.out.println("g(get): get first element");
System.out.println("p(peek): peek first element");
System.out.println("e(exit): exit the program");
key = scanner.next().charAt(0);
switch (key) {
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("Please enter the element you'd like to add:");
arrayQueue.addQueue(scanner.nextInt());
break;
case 'g':
try {
System.out.println("The element is: " + arrayQueue.getQueue());
System.out.println();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
break;
case 'p':
try {
System.out.println("The first element is: " + arrayQueue.peekQueue());
System.out.println();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
break;
case 'e':
loop = false;
break;
default:
break;
}
}
System.out.println("Thanks for your using!");
}
}