-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQueueDS.java
147 lines (140 loc) · 3.93 KB
/
QueueDS.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// https://excalidraw.com/#json=uMB2i0Ia7d-GvRTdvY1Kt,-GEnF7Q-0LWW7rKQsj2Apg
// 1. Queue
// 2. circular queue
// 3. Dequeue
// 4. Sliding window using Deque
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
public class QueueDS {
static class Q {
private int front;
private int rear;
private int size;
int[] arr; // just declared, not defined
public Q(int s) {
this.size = s;
arr = new int[size];
front = -1;
rear = -1;
}
public void push(int v) {
// for handling size limitations
if (rear == size-1) {
System.out.print("queue already full");
return;
}
arr[++rear] = v;
if(front == -1) {
// queue was empty
front++;
}
}
public int pop() {
// to handle size
if (front == -1) {
// it means no element present
return -1;
}
int val = arr[front++];
if (front > rear) {
// queue has now become empty
front = rear = -1;
}
return val;
}
public int front() {
// to handle size
if (front == -1) {
// it means no element present
return -1;
}
return arr[front];
}
public boolean isEmpty() {
if (front > rear || (front == -1 && rear == -1)) {
return true;
}
return false;
}
}
static class CircularQ {
private int front;
private int rear;
private int size;
int[] arr; // just declared, not defined
public CircularQ(int s) {
this.size = s;
arr = new int[size];
front = -1; // front points to location where front element is present
rear = -1; // rear points to location where last element is present
}
private boolean isFull() {
if ((rear+1)%size == front) {
return true;
}
return false;
}
public void push(int v) {
// check if queue is full
if (isFull()) {
System.out.print("queue already full");
return;
}
rear = (rear+1)%size;
arr[rear] = v;
if (front == -1) {
// it means previously queue was empty
front = rear;
}
}
public int pop() {
if (isEmpty()) {
System.out.print("queue already empty");
return -1;
}
int val = arr[front];
front = (front+1)%size;
if ((rear+1)%size == front) {
// queue has become empty
front = rear = -1;
}
return val;
}
public int front() {
// to handle size
if (isEmpty()) {
// it means no element present
return -1;
}
return arr[front];
}
public boolean isEmpty() {
if(front == -1) {
return true;
}
return false;
}
}
public static void main(String[] args) {
Q q = new Q(6);
q.push(1);
q.push(2);
q.push(3);
System.out.print(q.pop());
System.out.print(q.pop());
System.out.print(q.pop());
System.out.print(q.pop());
Queue<Integer> ourQ = new LinkedList<>();
ourQ.add(6); // pushing
ourQ.poll(); // front
ourQ.remove(); // popping
ourQ.isEmpty();
Deque<Integer> dq = new LinkedList<>();
dq.addFirst(6);
dq.addLast(10);
dq.removeFirst();
dq.removeLast();
dq.isEmpty();
}
}