-
Notifications
You must be signed in to change notification settings - Fork 9
/
PersonQ.java
62 lines (55 loc) · 1.27 KB
/
PersonQ.java
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
public class PersonQ {
private Person [] q;
private int size;
private int total;
private int front;
private int rear;
public PersonQ() {
size = 100;
total = 0;
front = 0;
rear = 0;
q = new Person[size];
}
public PersonQ(int size) {
this.size = size;
total = 0;
front = 0;
rear = 0;
q = new Person[size];
}
public boolean enqueue(Person item) {
if (isFull()) {
return false;
} else {
total++;
q[rear] = item;
//rear++; there is a problem!!
rear = (rear + 1) % size;//in order to back to
return true;
}
}
public Person dequeue() {
Person item = q[front];
total--;
//front ++; there is a problem too
front = (front + 1) % size;
return item;
}
public boolean isFull() {
if (size == total) {
return true;
} else {
return false;
}
}
public void showAll() {
int f = front;
if (total != 0) {
for (int i = 0; i < total; i++) {
System.out.println("" + q[f]);
f = (f + 1) % size;
}
}
}
}