Using arrays to implement queues (JAVA)

Last updated on December 28, 2022 pm

队列(Queue):有序列表,可以用数组或链表实现。先入先出。

数组模拟普通队列

front: point to the previous place of the first element.

rear: point to the last element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
public class ArrayQueueDemo {
public static void main(String[] args) {
//测试功能
ArrayQueue arrayQueue = new ArrayQueue(3);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("s:show 显示队列");
System.out.println("e:exit 退出程序");
System.out.println("a:add 添加数据到队列");
System.out.println("g:get 从队列中取出数据");
System.out.println("h:head 从队列中读取头数据");
key = scanner.next().charAt(0);
switch (key) {
case 's':
arrayQueue.showQueue();
break;
case 'e':
scanner.close();
System.out.println("Exit the program.");
loop = false;
break;
case 'a':
System.out.println("Please enter a number:");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int v = arrayQueue.getQueue();
System.out.println("You get the value: " + v);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int result = arrayQueue.peed();
System.out.println("The head of queue is " + result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
break;
}
}
}

//使用数组模拟普通队列
//并未模拟环形队列,因此此数组队列空间只能利用一次
static class ArrayQueue {
private int maxSize; //数组的最大容量
private int front; //队列头
private int rear; //队列尾
private int[] arr; //用于存储数据,模拟队列

//构造器
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1;
rear = -1;
}

//判断队列是否已满
public boolean isFull() {
return rear == maxSize - 1;
}

//判断队列是否为空
public boolean isEmpty() {
return front == rear;
}

//添加数据
public void addQueue(int n) {
if (isFull()) {
System.out.println("This queue is full and you cannot add data in it.");
return;
}
rear++;
arr[rear] = n;
}

//出队列
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("This queue is empty. You cannot get data from it.");
}
front++;
return arr[front];
}

//显示队列的所有数据
public void showQueue() {
if (isEmpty()) {
System.out.println("This queue is empty.");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d] = %d\n", i, arr[i]);
}
}

//显示队列头数据
public int peed() {
if (isEmpty()) {
throw new RuntimeException("This queue is empty.");
}
return arr[front + 1];
}
}
}

数组模拟环形队列(Circular Queue)

front: point to the first element. Initiated as 0.

rear: point to the place after the last element. Initiated as 0.

isFull: (rear + 1) % maxSize == front

isEmpty: rear == front

size: (rear - front + maxSize) % maxSize

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//测试功能
circleArray circleArray = new circleArray(4);
char key;
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("s:show 显示队列");
System.out.println("e:exit 退出程序");
System.out.println("a:add 添加数据到队列");
System.out.println("g:get 从队列中取出数据");
System.out.println("h:head 从队列中读取头数据");
key = scanner.next().charAt(0);
switch (key) {
case 's':
circleArray.showQueue();
break;
case 'e':
scanner.close();
System.out.println("Exit the program.");
loop = false;
break;
case 'a':
System.out.println("Please enter a number:");
int value = scanner.nextInt();
circleArray.addQueue(value);
break;
case 'g':
try {
int v = circleArray.getQueue();
System.out.println("You get the value: " + v);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int result = circleArray.peed();
System.out.println("The head of queue is " + result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
break;
}
}
}

//数组模拟环形队列,可重复使用空间
static class circleArray {
private int maxSize; //数组的最大容量
private int front; //队列头
private int rear; //队列尾
private int[] arr; //用于存储数据,模拟队列

//构造器
public circleArray(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = 0;
rear = 0;
}

//判断队列是否已满
public boolean isFull() {
return (rear + 1) % maxSize == front;
}

//判断队列是否为空
public boolean isEmpty() {
return front == rear;
}

//添加数据
public void addQueue(int n) {
if (isFull()) {
System.out.println("This queue is full and you cannot add data in it.");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}

//出队列
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("This queue is empty. You cannot get data from it.");
}
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}

//求有效数据个数
public int size() {
return (rear - front + maxSize) % maxSize;
}

//显示队列的所有数据
public void showQueue() {
if (isEmpty()) {
System.out.println("This queue is empty.");
return;
}
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d] = %d\n", i % maxSize, arr[i % maxSize]);
}
}

//显示队列头数据
public int peed() {
if (isEmpty()) {
throw new RuntimeException("This queue is empty.");
}
return arr[front];
}

}
}


Using arrays to implement queues (JAVA)
http://hihiko.zxy/2022/12/28/Using-arrays-to-implement-queues/
Author
Posted on
December 28, 2022
Licensed under