-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueueDemo.java
More file actions
127 lines (113 loc) · 3.83 KB
/
ArrayQueueDemo.java
File metadata and controls
127 lines (113 loc) · 3.83 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
package com.yijie.queue;
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
//test
//create a queue
ArrayQueue queue = new ArrayQueue(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!");
}
}
//use array to build a queue: ArrayQueue
class ArrayQueue {
private int maxSize;// maximum size of the queue
private int front;//the front of the queue
private int rear;//the rear of the queue
private int[] arr;//used to store the values
//create the constructor of the queue
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1;//point to the head of the queue,i.e. to the previous position at the head of the queue
rear = -1;//point directly to the rear of the queue, i.e. to the last position of the queue
}
//determine if the queue is full
public boolean isFull() {
return rear == maxSize - 1;
}
//determine if the queue is empty
public boolean isEmpty() {
return rear == front;
}
//add the item to the queue
public void addQueue(int n) {
//determine if full
if (isFull()) {
System.out.println("Queue is full!");
return;
}
rear++;
arr[rear] = n;
}
//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!");
}
front++;//move the front to the next position
return arr[front];
}
//display all the values of the queue
public void showQueue() {
//traverse
if (isEmpty()) {
System.out.println("Empty!");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
// display the items at the head of the queue, not get
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("Empty!");
}
return arr[front + 1];
}
}