-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueB.java
209 lines (189 loc) · 4.95 KB
/
QueueB.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import java.util.*;
public class QueueB {
// Queue using array
static class QueueA{
int arr[];
int size;
int rear;
QueueA(int n){
arr =new int[n];
rear=-1;
size=n;
}
public boolean isEmpty(){
return rear==-1;
}
public void add(int data){
if (rear==size-1){
System.out.println("Queue is full");
return;
}
rear++;
arr[rear]=data;
}
public int remove(){
if (isEmpty()) {
System.out.println("Queue is Empty");
return -1 ;
}
int front =arr[0];
for (int i = 0; i < rear; i++) {
arr[i]=arr[i+1];
}
rear--;
return front;
}
public int peek(){
return arr[0];
}
}
// Circular Queue using Array
static class QueueACircular{
static int arr[];
static int rear;
static int front;
static int size;
QueueACircular(int n){
arr = new int[n];
rear=-1;
front=-1;
size = n;
}
boolean isEmpty(){
return rear==-1&&front==-1;
}
boolean isFull(){
return (rear+1)%size==front;
}
void add(int data){
if(isFull()){
System.out.println("Queue is FULL!");
return;
}
if (front==-1) {
front=0;
}
rear=(rear+1)%size;
arr[rear]=data;
}
int remove(){
if (isEmpty()) {
System.out.print("Queue is Empty");
return -1;
}
int f= arr[front];
if (front==rear) {
rear=front=-1;
} else {
front = (front+1)%size;
}
return f;
}
int peek(){
if (isEmpty()) {
System.out.println("It's Empty");
return -1;
}
return arr[front];
}
}
// Queue Using LinkedList
static class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
static class QueueLL{
static Node head=null;
static Node tail = null;
public boolean isEmpty(){
return head==null && tail==null;
}
public void add(int data){
Node newNode = new Node(data);
if(head==null){
head = tail = newNode;
return;
}
tail.next=newNode;
tail = newNode;
}
public void remove(){
if (head==tail) {
head=tail=null;
return;
}
head=head.next;
}
public int peek(){
if(isEmpty()){
System.out.print("Queue is Empty");
return -1;
}
return head.data;
}
}
// ************************* Questions *************************** //
// interleave two halves of queue
static void interleave(Queue<Integer> q){
// this will work only for even nuber of elements in Queue
// Uncomment the below if conditions to run it for both even and odd
Queue<Integer> q2 = new LinkedList<>();
int halp = q.size()/2;
// if (q.size()%2!=0) {
// halp++;
// }
for (int i = 0; i < halp; i++) {
q2.add(q.remove());
}
while (!q2.isEmpty()) {
q.add(q2.remove());
// if (q2.isEmpty()&&q.size()%2!=0) {
// break;
// }
q.add(q.remove());
}
}
static void reverse(Queue<Integer> q){
Stack<Integer> s = new Stack<>();
while (!q.isEmpty()) {
s.push(q.remove());
}
while (!s.isEmpty()) {
q.add(s.pop());
}
}
public static void main(String[] args) {
// QueueA q = new QueueA(1);
// QueueACircular q= new QueueACircular(9);
// QueueLL q = new QueueLL();
// Queue<Integer> q2 = new LinkedList<>();
// q2.add(1);
// q2.add(2);
// q2.add(3);
// q2.add(4);
// q2.add(5);
// q2.add(6);
// q2.add(7);
// System.out.println(q2);
// reverse(q2);
// System.out.println(q2);
// Queue<Integer> q1 = new LinkedList<>();
// q1.add(1);
// q1.add(2);
// q1.add(3);
// q1.add(4);
// q1.add(5);
// q1.add(6);
// q1.add(7);
// q1.add(8);
// q1.add(9);
// q1.add(10);
// System.out.println(q1);
// interleave(q1);
// System.out.println(q1);
}
}