-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
58 lines (46 loc) · 958 Bytes
/
Copy pathQueue.java
File metadata and controls
58 lines (46 loc) · 958 Bytes
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
public class Queue {
int[] arr;
int front;
int rear;
int size;
int capacity;
public Queue(int cap) {
capacity = cap;
arr = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
public void enqueue(int x) {
if (isFull())
return;
rear++;
arr[rear] = x;
size++;
}
public int dequeue() {
if (isEmpty())
return -1;
int val = arr[front];
front++;
size--;
return val;
}
public int peek() {
if (isEmpty())
return -1;
return arr[front];
}
public void printQueue() {
for (int i = front; i <= rear; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}