11/**
22 * This implements Queues by using the class Queue.
3- *
3+ * <p>
44 * A queue data structure functions the same as a real world queue.
55 * The elements that are added first are the first to be removed.
66 * New elements are added to the back/rear of the queue.
7- *
8- * @author Unknown
97 *
8+ * @author Unknown
109 */
11- class Queue {
12- /** Max size of the queue */
13- private int maxSize ;
14- /** The array representing the queue */
15- private int [] queueArray ;
16- /** Front of the queue */
17- private int front ;
18- /** Rear of the queue */
19- private int rear ;
20- /** How many items are in the queue */
21- private int nItems ;
22-
23- /**
24- * Constructor
25- *
26- * @param size Size of the new queue
27- */
28- public Queue (int size ){
29- maxSize = size ;
30- queueArray = new int [size ];
31- front = 0 ;
32- rear = -1 ;
33- nItems = 0 ;
34- }
35-
36- /**
37- * Inserts an element at the rear of the queue
38- *
39- * @param x element to be added
40- * @return True if the element was added successfully
41- */
42- public boolean insert (int x ){
43- if (isFull ())
44- return false ;
45- if (rear == maxSize -1 ) //If the back of the queue is the end of the array wrap around to the front
46- rear = -1 ;
47- rear ++;
48- queueArray [rear ] = x ;
49- nItems ++;
50- return true ;
51- }
52-
53- /**
54- * Remove an element from the front of the queue
55- *
56- * @return the new front of the queue
57- */
58- public int remove (){ //Remove an element from the front of the queue
59- if (isEmpty ()){
60- System .out .println ("Queue is empty" );
61- return -1 ;
62- }
63- int temp = queueArray [front ];
64- front ++;
65- if (front == maxSize ) //Dealing with wrap-around again
66- front = 0 ;
67- nItems --;
68- return temp ;
69- }
70-
71- /**
72- * Checks what's at the front of the queue
73- *
74- * @return element at the front of the queue
75- */
76- public int peekFront (){
77- return queueArray [front ];
78- }
79-
80- /**
81- * Checks what's at the rear of the queue
82- *
83- * @return element at the rear of the queue
84- */
85- public int peekRear (){
86- return queueArray [rear ];
87- }
88-
89- /**
90- * Returns true if the queue is empty
91- *
92- * @return true if the queue is empty
93- */
94- public boolean isEmpty (){
95- return (nItems == 0 );
96- }
97-
98- /**
99- * Returns true if the queue is full
100- *
101- * @return true if the queue is full
102- */
103- public boolean isFull (){
104- return (nItems == maxSize );
105- }
106-
107- /**
108- * Returns the number of elements in the queue
109- *
110- * @return number of elements in the queue
111- */
112- public int getSize (){
113- return nItems ;
114- }
10+ class Queue {
11+ /**
12+ * Max size of the queue
13+ */
14+ private int maxSize ;
15+ /**
16+ * The array representing the queue
17+ */
18+ private int [] queueArray ;
19+ /**
20+ * Front of the queue
21+ */
22+ private int front ;
23+ /**
24+ * Rear of the queue
25+ */
26+ private int rear ;
27+ /**
28+ * How many items are in the queue
29+ */
30+ private int nItems ;
31+
32+ /**
33+ * Constructor
34+ *
35+ * @param size Size of the new queue
36+ */
37+ public Queue (int size ) {
38+ maxSize = size ;
39+ queueArray = new int [size ];
40+ front = 0 ;
41+ rear = -1 ;
42+ nItems = 0 ;
43+ }
44+
45+ /**
46+ * Inserts an element at the rear of the queue
47+ *
48+ * @param x element to be added
49+ * @return True if the element was added successfully
50+ */
51+ public boolean insert (int x ) {
52+ if (isFull ())
53+ return false ;
54+ if (rear == maxSize - 1 ) // If the back of the queue is the end of the array wrap around to the front
55+ rear = -1 ;
56+ rear ++;
57+ queueArray [rear ] = x ;
58+ nItems ++;
59+ return true ;
60+ }
61+
62+ /**
63+ * Remove an element from the front of the queue
64+ *
65+ * @return the new front of the queue
66+ */
67+ public int remove () { // Remove an element from the front of the queue
68+ if (isEmpty ()) {
69+ System .out .println ("Queue is empty" );
70+ return -1 ;
71+ }
72+ int temp = queueArray [front ];
73+ front ++;
74+ if (front == maxSize ) //Dealing with wrap-around again
75+ front = 0 ;
76+ nItems --;
77+ return temp ;
78+ }
79+
80+ /**
81+ * Checks what's at the front of the queue
82+ *
83+ * @return element at the front of the queue
84+ */
85+ public int peekFront () {
86+ return queueArray [front ];
87+ }
88+
89+ /**
90+ * Checks what's at the rear of the queue
91+ *
92+ * @return element at the rear of the queue
93+ */
94+ public int peekRear () {
95+ return queueArray [rear ];
96+ }
97+
98+ /**
99+ * Returns true if the queue is empty
100+ *
101+ * @return true if the queue is empty
102+ */
103+ public boolean isEmpty () {
104+ return (nItems == 0 );
105+ }
106+
107+ /**
108+ * Returns true if the queue is full
109+ *
110+ * @return true if the queue is full
111+ */
112+ public boolean isFull () {
113+ return (nItems == maxSize );
114+ }
115+
116+ /**
117+ * Returns the number of elements in the queue
118+ *
119+ * @return number of elements in the queue
120+ */
121+ public int getSize () {
122+ return nItems ;
123+ }
115124}
116125
117126/**
118127 * This class is the example for the Queue class
119- *
120- * @author Unknown
121128 *
129+ * @author Unknown
122130 */
123- public class Queues {
124- /**
125- * Main method
126- *
127- * @param args Command line arguments
128- */
129- public static void main (String args []){
130- Queue myQueue = new Queue (4 );
131- myQueue .insert (10 );
132- myQueue .insert (2 );
133- myQueue .insert (5 );
134- myQueue .insert (3 );
135- // [10(front), 2, 5, 3(rear)]
136-
137- System .out .println (myQueue .isFull ()); //Will print true
138-
139- myQueue .remove (); //Will make 2 the new front, making 10 no longer part of the queue
140- // [10, 2(front), 5, 3(rear)]
141-
142- myQueue .insert (7 ); //Insert 7 at the rear which will be index 0 because of wrap around
143- // [7(rear), 2(front), 5, 3]
144-
145- System .out .println (myQueue .peekFront ()); //Will print 2
146- System .out .println (myQueue .peekRear ()); //Will print 7
147- }
148- }
131+ public class Queues {
132+ /**
133+ * Main method
134+ *
135+ * @param args Command line arguments
136+ */
137+ public static void main (String args []) {
138+ Queue myQueue = new Queue (4 );
139+ myQueue .insert (10 );
140+ myQueue .insert (2 );
141+ myQueue .insert (5 );
142+ myQueue .insert (3 );
143+ // [10(front), 2, 5, 3(rear)]
144+
145+ System .out .println (myQueue .isFull ()); // Will print true
146+
147+ myQueue .remove (); // Will make 2 the new front, making 10 no longer part of the queue
148+ // [10, 2(front), 5, 3(rear)]
149+
150+ myQueue .insert (7 ); // Insert 7 at the rear which will be index 0 because of wrap around
151+ // [7(rear), 2(front), 5, 3]
152+
153+ System .out .println (myQueue .peekFront ()); // Will print 2
154+ System .out .println (myQueue .peekRear ()); // Will print 7
155+ }
156+ }
0 commit comments