-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.java
More file actions
79 lines (68 loc) · 1.52 KB
/
ArrayQueue.java
File metadata and controls
79 lines (68 loc) · 1.52 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
// Wenlu Cheng, CSE 373
// assignment #1, ID:1336340
// This file is an arrayQueue that can be used to store oracle's questions
// in the executor.java file
public class ArrayQueue {
private String[] queueArray;
private int size;
private int front;
private int back;
public ArrayQueue(){
queueArray = new String[100];
size = 0;
front = 0;
back = -1;
}
public ArrayQueue(int startSize){
queueArray = new String[startSize];
size = 0;
front = 0;
back = -1;
}
/**
* @function returns the number of elements in the queue
* @return size
*/
public int getSize(){
return size;
}
/**
* @function adds a string to the end of the queue
* @param toEnqueue: the input to be inserted
*/
public void enqueue(String toEnqueue){
if(isFull())
System.out.print("Queue is full");
// if not full, keep enqueuing
back = (back + 1) % (queueArray.length);
queueArray[back] = toEnqueue;
size++;
}
/**
* @function removes the string from the front of the queue
* @return the string from the front of the queue
*/
public String dequeue(){
if(isEmpty())
return null;
// if not empty, keep dequeuing
String toDequeue = queueArray[front];
front = (front + 1) % queueArray.length;
size--;
return toDequeue;
}
/**
*
* @return returns true if the queue is empty, false otherwise
*/
public boolean isEmpty(){
return (size == 0);
}
/**
*
* @return returns true if the queue is full, false otherwise
*/
public boolean isFull(){
return (size == queueArray.length);
}
}