-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircleArrayQueueDemo.java
More file actions
141 lines (123 loc) · 4.14 KB
/
CircleArrayQueueDemo.java
File metadata and controls
141 lines (123 loc) · 4.14 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.yijie.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//test
//create a circle queue
CircleArray queue = new CircleArray(4);
char key = ' ';//receive the user input
Scanner scanner = new Scanner(System.in);
boolean loop = true;
//output a menu
while (loop) {
System.out.println("s(show): display the queue");
System.out.println("e(exit): quit");
System.out.println("a(add): add data to the queue");
System.out.println("g(get): get the data from the queue");
System.out.println("h(head): display the head of the queue");
key = scanner.next().charAt(0);//receive the user input
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("Please enter a whole number:");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
int res = queue.getQueue();
System.out.printf("The fetched data is: %d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.headQueue();
System.out.printf("The head is: %d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e'://quit
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("Program ends!");
}
}
class CircleArray {
private int maxSize;// maximum size of the queue
//front points to the first item of the queue, i.e. arr[front] is the head of the queue
//initialize front = 0
private int front;
//rear points to the next position of the last item in the queue
//initialize rear = 0
private int rear;
private int[] arr;//used to store the values
public CircleArray(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = 0;
rear = 0;
}
//determine if the queue is full
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
//determine if the queue is empty
public boolean isEmpty() {
return rear == front;
}
//add the item to the queue
public void addQueue(int n) {
//determin if full
if (isFull()) {
System.out.println("Queue is full!");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
//get the item of the queue
public int getQueue() {
//determine if empty
if (isEmpty()) {
throw new RuntimeException("Queue is empty, can't get the data!");
}
//1. assign the value of front to a temporary variable
//2. move the front to the next position, considering %
//3. return the temporary variable
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
//display all the values of the queue
public void showQueue() {
//traverse
if (isEmpty()) {
System.out.println("Empty!");
return;
}
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
//figure out the effective number of the items in the queue
public int size() {
return (rear + maxSize - front) % maxSize;
}
// display the items at the head of the queue, not get
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("Empty!");
}
return arr[front];
}
}