Repository navigation
Expand file tree
/
Copy pathQueue.java
More file actions
62 lines (48 loc) · 1.43 KB
/
Copy pathQueue.java
File metadata and controls
62 lines (48 loc) · 1.43 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
package algoBootcamp.ds.queue;
public class Queue {
private int maxSize; // initializes the set number of slots
private long[] queArray; // slots to algoBootcamp the data
private int front; // this will be the index position for the element in the front
private int rear; // going to be the index position for the element at the back of the line
private int nItems; // counter to maintain the number of items in our queue
public Queue(int size){
this.maxSize = size;
this.queArray = new long[size];
front = 0; // index position of the first slot of the array
rear = -1; // there is no item in the array yet to be considered as the last item.
nItems =0; // we don't have elements in the array yet
}
public void insert(long j){
if(rear == maxSize -1){
rear = -1;
}
rear++;
queArray[rear] = j;
nItems ++;
}
public long remove(){ // remove item from the front of the queue
long temp = queArray[front];
front++;
if(front == maxSize){
front = 0; // we can set front back to the 0th index so that we can utilize the entire array again
}
nItems --;
return temp;
}
public long peekFront(){
return queArray[front];
}
public boolean isEmpty(){
return (nItems ==0);
}
public boolean isFull(){
return (nItems == maxSize);
}
public void view(){
System.out.print("[ ");
for(int i = 0; i < queArray.length; i++){
System.out.print(queArray[i]+ " ");
}
System.out.print("]");
}
}