11/**
22 * This class implements a PriorityQueue.
3- *
3+ * <p>
44 * A priority queue adds elements into positions based on their priority.
55 * So the most important elements are placed at the front/on the top.
66 * In this example I give numbers that are bigger, a higher priority.
77 * Queues in theory have no fixed size but when using an array
88 * implementation it does.
9- *
10- * @author Unknown
119 *
1210 */
13- class PriorityQueue {
14- /** The max size of the queue */
15- private int maxSize ;
16- /** The array for the queue */
17- private int [] queueArray ;
18- /** How many items are in the queue */
19- private int nItems ;
11+ class PriorityQueue {
12+ /**
13+ * The max size of the queue
14+ */
15+ private int maxSize ;
16+ /**
17+ * The array for the queue
18+ */
19+ private int [] queueArray ;
20+ /**
21+ * How many items are in the queue
22+ */
23+ private int nItems ;
2024
21- /**
22- * Constructor
23- *
24- * @param size Size of the queue
25- */
26- public PriorityQueue (int size ){
27- maxSize = size ;
28- queueArray = new int [size ];
29- nItems = 0 ;
30- }
25+ /**
26+ * Constructor
27+ *
28+ * @param size Size of the queue
29+ */
30+ public PriorityQueue (int size ) {
31+ maxSize = size ;
32+ queueArray = new int [size ];
33+ nItems = 0 ;
34+ }
3135
32- /**
33- * Inserts an element in it's appropriate place
34- *
35- * @param value Value to be inserted
36- */
37- public void insert (int value ){
38- if (nItems == 0 ){
39- queueArray [0 ] = value ;
40- }
41- else {
42- int j = nItems ;
43- while (j > 0 && queueArray [j -1 ] > value ){
44- queueArray [j ] = queueArray [j -1 ]; //Shifts every element up to make room for insertion
45- j --;
46- }
47- queueArray [j ] = value ; //Once the correct position is found the value is inserted
48- }
49- nItems ++;
50- }
36+ /**
37+ * Inserts an element in it's appropriate place
38+ *
39+ * @param value Value to be inserted
40+ */
41+ public void insert (int value ) {
42+ if (isFull ()) {
43+ throw new RuntimeException ("Queue is full" );
44+ }
45+ if (nItems == 0 ) {
46+ queueArray [0 ] = value ;
47+ } else {
48+ int j = nItems ;
49+ while (j > 0 && queueArray [j - 1 ] > value ) {
50+ queueArray [j ] = queueArray [j - 1 ]; // Shifts every element up to make room for insertion
51+ j --;
52+ }
53+ queueArray [j ] = value ; // Once the correct position is found the value is inserted
54+ }
55+ nItems ++;
56+ }
5157
52- /**
53- * Remove the element from the front of the queue
54- *
55- * @return The element removed
56- */
57- public int remove (){
58- return queueArray [--nItems ];
59- }
58+ /**
59+ * Remove the element from the front of the queue
60+ *
61+ * @return The element removed
62+ */
63+ public int remove () {
64+ return queueArray [--nItems ];
65+ }
6066
61- /**
62- * Checks what's at the front of the queue
63- *
64- * @return element at the front of the queue
65- */
66- public int peek (){
67- return queueArray [nItems - 1 ];
68- }
67+ /**
68+ * Checks what's at the front of the queue
69+ *
70+ * @return element at the front of the queue
71+ */
72+ public int peek () {
73+ return queueArray [nItems - 1 ];
74+ }
6975
70- /**
71- * Returns true if the queue is empty
72- *
73- * @return true if the queue is empty
74- */
75- public boolean isEmpty (){
76- return (nItems == 0 );
77- }
76+ /**
77+ * Returns true if the queue is empty
78+ *
79+ * @return true if the queue is empty
80+ */
81+ public boolean isEmpty () {
82+ return (nItems == 0 );
83+ }
7884
79- /**
80- * Returns true if the queue is full
81- *
82- * @return true if the queue is full
83- */
84- public boolean isFull (){
85- return (nItems == maxSize );
86- }
85+ /**
86+ * Returns true if the queue is full
87+ *
88+ * @return true if the queue is full
89+ */
90+ public boolean isFull () {
91+ return (nItems == maxSize );
92+ }
8793
88- /**
89- * Returns the number of elements in the queue
90- *
91- * @return number of elements in the queue
92- */
93- public int getSize (){
94- return nItems ;
95- }
94+ /**
95+ * Returns the number of elements in the queue
96+ *
97+ * @return number of elements in the queue
98+ */
99+ public int getSize () {
100+ return nItems ;
101+ }
96102}
97103
98104/**
99105 * This class implements the PriorityQueue class above.
100- *
101- * @author Unknown
102106 *
107+ * @author Unknown
103108 */
104- public class PriorityQueues {
105- /**
106- * Main method
107- *
108- * @param args Command Line Arguments
109- */
110- public static void main (String args []) {
111- PriorityQueue myQueue = new PriorityQueue (4 );
112- myQueue .insert (10 );
113- myQueue .insert (2 );
114- myQueue .insert (5 );
115- myQueue .insert (3 );
116- // [2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
109+ public class PriorityQueues {
110+ /**
111+ * Main method
112+ *
113+ * @param args Command Line Arguments
114+ */
115+ public static void main (String [] args ) {
116+ PriorityQueue myQueue = new PriorityQueue (4 );
117+ myQueue .insert (10 );
118+ myQueue .insert (2 );
119+ myQueue .insert (5 );
120+ myQueue .insert (3 );
121+ // [2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
117122
118- for (int i = 3 ; i >= 0 ; i --)
119- System .out .print (myQueue .remove () + " " ); //will print the queue in reverse order [10, 5, 3, 2]
123+ for (int i = 3 ; i >= 0 ; i --)
124+ System .out .print (myQueue .remove () + " " ); // will print the queue in reverse order [10, 5, 3, 2]
120125
121- // As you can see, a Priority Queue can be used as a sorting algotithm
122- }
123- }
126+ // As you can see, a Priority Queue can be used as a sorting algotithm
127+ }
128+ }
0 commit comments