1
+ import java .util .*;
2
+
3
+ abstract class ArrayPriorQueue
4
+ {
5
+ // class for creating QueueOverflow exception
6
+ private static class QueueOverflowException extends Exception
7
+ {
8
+ private static final long serialVersionUID = 1L ;
9
+
10
+ public QueueOverflowException (String message )
11
+ {
12
+ super (message );
13
+ }
14
+ public String toString ()
15
+ {
16
+ return "myjava.QueueOverflowException: " +getMessage ();
17
+ }
18
+ }
19
+
20
+ //class for creating Empty Queue exception
21
+ private static class EmptyQueueException extends Exception
22
+ {
23
+ private static final long serialVersionUID = 1L ;
24
+
25
+ public EmptyQueueException (String str )
26
+ {
27
+ super (str );
28
+ }
29
+ public String toString ()
30
+ {
31
+ return "myjava.EmptyQueueException: " +getMessage ();
32
+ }
33
+ }
34
+
35
+ // Node class for storing data with priorities
36
+ private static class Node
37
+ {
38
+ private int priority ; // priority of data
39
+ private int data ; // data in the node
40
+
41
+ public Node (int data ,int priority ) // constructor
42
+ {
43
+ this .data =data ;
44
+ this .priority = priority ;
45
+ }
46
+
47
+ /**
48
+ * It will return the priority of the node
49
+ *
50
+ * @return int for priority
51
+ **/
52
+ public int getPriority ()
53
+ {
54
+ return priority ;
55
+ }
56
+
57
+ /**
58
+ * It will return the data of the node
59
+ *
60
+ * @return int for data
61
+ **/
62
+ public int getData ()
63
+ {
64
+ return data ;
65
+ }
66
+ }
67
+
68
+ // class for priority queue
69
+ private static class PriorQueue
70
+ {
71
+ private Node [] arr ; // array for queue
72
+ private int capacity ; // for setting capacity of the queue
73
+ private int front ; // front index of the queue
74
+ private int rear ; // rear index of the queue
75
+
76
+ /**
77
+ * Valued Constructor for making queue with the given capacity
78
+ *
79
+ * @param int for capacity
80
+ **/
81
+ public PriorQueue (int capacity )
82
+ {
83
+ this .capacity = capacity ;
84
+ arr = new Node [capacity ];
85
+ front =-1 ;
86
+ rear =-1 ;
87
+ }
88
+
89
+ /**
90
+ * Default Constructor for making the queue with default size i.e 10
91
+ **/
92
+ public PriorQueue ()
93
+ {
94
+ this (10 );
95
+ }
96
+
97
+ /**
98
+ * This function will add one element to the queue according to the priority
99
+ *
100
+ * @param int for data
101
+ * @param int for priority
102
+ * @exception QueueOverflowException
103
+ **/
104
+ public void enqueue (int data ,int priority ) throws QueueOverflowException
105
+ {
106
+ if (rear +1 ==capacity )
107
+ {
108
+ throw new QueueOverflowException ("Queue Overflow !!!" );
109
+ }
110
+ Node newNode = new Node (data ,priority );
111
+ if (rear ==-1 && front ==-1 )
112
+ {
113
+ front ++;
114
+ rear ++;
115
+ arr [rear ] = newNode ;
116
+ }
117
+ else if (priority <= arr [0 ].getPriority ()) //search for correct position in the array according to priority
118
+ {
119
+ //shift the array
120
+ rear ++;
121
+ for (int i =rear ;i >0 ;i --)
122
+ {
123
+ arr [i ] = arr [i -1 ];
124
+ }
125
+ arr [0 ] = newNode ; // insert at front
126
+ }
127
+ else if (priority >= arr [rear ].getPriority ()) // insert at last
128
+ {
129
+ rear ++;
130
+ arr [rear ] = newNode ;
131
+ }
132
+ else // insert in between
133
+ {
134
+ //search position in the array for insertion
135
+ int i =-1 ;
136
+ for (i =0 ;i <=rear ;i ++)
137
+ {
138
+ if (priority <= arr [i ].getPriority ())
139
+ {
140
+ break ;
141
+ }
142
+ }
143
+ rear ++;
144
+ for (int j =rear ;j >i ;j --) // shift half right array to right
145
+ {
146
+ arr [j ] = arr [j -1 ];
147
+ }
148
+ arr [i ] = newNode ; // insert the node
149
+ }
150
+ }
151
+
152
+ /**
153
+ * This function will remove an element from priority queue
154
+ *
155
+ * @return int for returning the removed element
156
+ * @exception EmptyQueueException
157
+ **/
158
+ public int dequeue () throws EmptyQueueException
159
+ {
160
+ if (front ==-1 || rear ==-1 )
161
+ {
162
+ throw new EmptyQueueException ("Empty Queue !!!" );
163
+ }
164
+ int result = arr [front ].getData ();
165
+ for (int i =0 ;i <rear ;i ++)
166
+ {
167
+ arr [i ] = arr [i +1 ];
168
+ }
169
+ rear --;
170
+ return result ;
171
+ }
172
+
173
+ /**
174
+ * This function will only return the front element of priority queue
175
+ *
176
+ * @return int for front element
177
+ * @exception EmptyQueueException
178
+ **/
179
+ public int peek () throws EmptyQueueException
180
+ {
181
+ if (front ==-1 || rear ==-1 )
182
+ {
183
+ throw new EmptyQueueException ("Empty Queue !!!" );
184
+ }
185
+ return arr [front ].getData ();
186
+ }
187
+
188
+ /**
189
+ * This function will print the queue
190
+ **/
191
+ public void print ()
192
+ {
193
+ for (int i =front ;i <=rear ;i ++)
194
+ {
195
+ System .out .println (arr [i ].getPriority () + " " + arr [i ].getData ());
196
+ }
197
+ }
198
+ }
199
+
200
+ public static void main (String [] args )
201
+ {
202
+ try
203
+ {
204
+ PriorQueue Q = new PriorQueue ();
205
+ Q .enqueue (12 , 1 );
206
+ Q .enqueue (31 , 1 );
207
+ Q .enqueue (13 , 0 );
208
+ Q .enqueue (24 , 4 );
209
+ Q .enqueue (34 , 8 );
210
+ Q .enqueue (100 , 3 );
211
+ Q .enqueue (200 , 3 );
212
+ Q .enqueue (120 , 0 );
213
+ Q .enqueue (202 , 5 );
214
+ Q .enqueue (223 , 9 );
215
+ // Q.enqueue(2323, 0);
216
+ Q .print ();
217
+ System .out .println (Q .dequeue ());
218
+ System .out .println (Q .dequeue ());
219
+ System .out .println (Q .dequeue ());
220
+ System .out .println (Q .dequeue ());
221
+ System .out .println (Q .dequeue ());
222
+ System .out .println (Q .dequeue ());
223
+ System .out .println (Q .dequeue ());
224
+ System .out .println (Q .dequeue ());
225
+ System .out .println (Q .dequeue ());
226
+ System .out .println (Q .dequeue ());
227
+ System .out .println (Q .peek ());
228
+ }
229
+ catch (Exception e )
230
+ {
231
+ System .out .println (e );
232
+ }
233
+ }
234
+ }
0 commit comments