-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDeque.java
More file actions
executable file
·51 lines (36 loc) · 1.13 KB
/
Copy pathArrayDeque.java
File metadata and controls
executable file
·51 lines (36 loc) · 1.13 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
package com.codeWithArsalon.LinearDS;
import java.util.Arrays;
//
public class ArrayDeque {
private int [] items;
private int rear;
private int front;
private int count;
public ArrayDeque(int capacity){
this.items = new int [capacity]; //pass in capacity of queue
}
public void enqueue(int item){
if(count == items.length)
throw new IllegalStateException();
items[rear] = item;
rear = (rear + 1) % items.length; //set rear at next index position (circular indexing formula)
count ++;
}
public int dequeue(){
var item = items[front]; //storing the item at front
items[front] = 0;
front = (front + 1) % items.length; //circular array index formula
count--;
return item; //returning the item replace by zero
}
public int peek (){
return items[front];
}
public boolean isEmpty(){
return count == 0;
}
@Override
public String toString(){
return Arrays.toString(items); //using Array class toString method to convert items array into a string.
}
}