-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathArrayPriorQueue.java
234 lines (218 loc) · 6.79 KB
/
ArrayPriorQueue.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import java.util.*;
abstract class ArrayPriorQueue
{
// class for creating QueueOverflow exception
private static class QueueOverflowException extends Exception
{
private static final long serialVersionUID = 1L;
public QueueOverflowException(String message)
{
super(message);
}
public String toString()
{
return "myjava.QueueOverflowException: "+getMessage();
}
}
//class for creating Empty Queue exception
private static class EmptyQueueException extends Exception
{
private static final long serialVersionUID = 1L;
public EmptyQueueException(String str)
{
super(str);
}
public String toString()
{
return "myjava.EmptyQueueException: "+getMessage();
}
}
// Node class for storing data with priorities
private static class Node
{
private int priority; // priority of data
private int data; // data in the node
public Node(int data,int priority) // constructor
{
this.data=data;
this.priority = priority;
}
/**
* It will return the priority of the node
*
* @return int for priority
**/
public int getPriority()
{
return priority;
}
/**
* It will return the data of the node
*
* @return int for data
**/
public int getData()
{
return data;
}
}
// class for priority queue
private static class PriorQueue
{
private Node[] arr; // array for queue
private int capacity; // for setting capacity of the queue
private int front; // front index of the queue
private int rear; // rear index of the queue
/**
* Valued Constructor for making queue with the given capacity
*
* @param int for capacity
**/
public PriorQueue(int capacity)
{
this.capacity = capacity;
arr = new Node[capacity];
front=-1;
rear=-1;
}
/**
* Default Constructor for making the queue with default size i.e 10
**/
public PriorQueue()
{
this(10);
}
/**
* This function will add one element to the queue according to the priority
*
* @param int for data
* @param int for priority
* @exception QueueOverflowException
**/
public void enqueue(int data,int priority) throws QueueOverflowException
{
if(rear+1==capacity)
{
throw new QueueOverflowException("Queue Overflow !!!");
}
Node newNode = new Node(data,priority);
if(rear==-1 && front==-1)
{
front++;
rear++;
arr[rear] = newNode;
}
else if(priority <= arr[0].getPriority()) //search for correct position in the array according to priority
{
//shift the array
rear++;
for(int i=rear;i>0;i--)
{
arr[i] = arr[i-1];
}
arr[0] = newNode; // insert at front
}
else if(priority >= arr[rear].getPriority()) // insert at last
{
rear++;
arr[rear] = newNode;
}
else // insert in between
{
//search position in the array for insertion
int i=-1;
for(i=0;i<=rear;i++)
{
if(priority <= arr[i].getPriority())
{
break;
}
}
rear++;
for(int j=rear;j>i;j--) // shift half right array to right
{
arr[j] = arr[j-1];
}
arr[i] = newNode; // insert the node
}
}
/**
* This function will remove an element from priority queue
*
* @return int for returning the removed element
* @exception EmptyQueueException
**/
public int dequeue() throws EmptyQueueException
{
if(front==-1 || rear==-1)
{
throw new EmptyQueueException("Empty Queue !!!");
}
int result = arr[front].getData();
for(int i=0;i<rear;i++)
{
arr[i] = arr[i+1];
}
rear--;
return result;
}
/**
* This function will only return the front element of priority queue
*
* @return int for front element
* @exception EmptyQueueException
**/
public int peek() throws EmptyQueueException
{
if(front==-1 || rear==-1)
{
throw new EmptyQueueException("Empty Queue !!!");
}
return arr[front].getData();
}
/**
* This function will print the queue
**/
public void print()
{
for(int i=front;i<=rear;i++)
{
System.out.println(arr[i].getPriority() + " " + arr[i].getData());
}
}
}
public static void main(String[] args)
{
try
{
PriorQueue Q = new PriorQueue();
Q.enqueue(12, 1);
Q.enqueue(31, 1);
Q.enqueue(13, 0);
Q.enqueue(24, 4);
Q.enqueue(34, 8);
Q.enqueue(100, 3);
Q.enqueue(200, 3);
Q.enqueue(120, 0);
Q.enqueue(202, 5);
Q.enqueue(223, 9);
// Q.enqueue(2323, 0);
Q.print();
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.peek());
}
catch(Exception e)
{
System.out.println(e);
}
}
}