-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcQueue.java
102 lines (83 loc) · 2.41 KB
/
ExcQueue.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
class Queue {
private int front;
private int rear;
private int maxSize;
private int arr[];
Queue(int maxSize) {
this.front = 0;
this.rear = -1;
this.maxSize = maxSize;
this.arr = new int[this.maxSize];
}
public boolean isFull() {
if (rear == maxSize - 1) {
return true;
}
return false;
}
public boolean enqueue(int data) {
if (isFull()) {
return false;
} else {
arr[++rear] = data;
return true;
}
}
public void display() {
if(isEmpty())
System.out.println("Queue is empty!");
else {
for (int index = front; index <= rear; index++) {
System.out.println(arr[index]);
}
}
}
public boolean isEmpty() {
if (front > rear)
return true;
return false;
}
public int dequeue() {
if (isEmpty()) {
return Integer.MIN_VALUE;
} else {
int data = arr[this.front];
arr[front++] = Integer.MIN_VALUE;
return data;
}
}
public int getMaxSize() {
return maxSize;
}
}
class QueueExercise {
public static void main(String[] args) {
Queue queue = new Queue(7);
queue.enqueue(2);
queue.enqueue(7);
queue.enqueue(9);
queue.enqueue(4);
queue.enqueue(6);
queue.enqueue(5);
queue.enqueue(10);
Queue[] queueArray = splitQueue(queue);
System.out.println("Elements in the queue of odd numbers");
queueArray[0].display();
System.out.println("\nElements in the queue of even numbers");
queueArray[1].display();
}
public static Queue[] splitQueue(Queue queue) {
Queue oddQueue = new Queue(queue.getMaxSize());
Queue evenQueue = new Queue(queue.getMaxSize());
while (!queue.isEmpty()) {
int data = queue.dequeue();
if (data % 2 == 0) {
evenQueue.enqueue(data);
} else {
oddQueue.enqueue(data);
}
}
Queue[] result = { oddQueue, evenQueue };
return result;
}
}