File tree Expand file tree Collapse file tree 1 file changed +3
-7
lines changed
Expand file tree Collapse file tree 1 file changed +3
-7
lines changed Original file line number Diff line number Diff line change @@ -53,9 +53,7 @@ public Queue(int size) {
5353 public boolean insert (int x ) {
5454 if (isFull ())
5555 return false ;
56- if (rear == maxSize - 1 ) // If the back of the queue is the end of the array wrap around to the front
57- rear = -1 ;
58- rear ++;
56+ rear = (rear + 1 ) % maxSize ; // If the back of the queue is the end of the array wrap around to the front
5957 queueArray [rear ] = x ;
6058 nItems ++;
6159 return true ;
@@ -72,9 +70,7 @@ public int remove() { // Remove an element from the front of the queue
7270 return -1 ;
7371 }
7472 int temp = queueArray [front ];
75- front ++;
76- if (front == maxSize ) //Dealing with wrap-around again
77- front = 0 ;
73+ front = (front + 1 ) % maxSize ;
7874 nItems --;
7975 return temp ;
8076 }
@@ -153,6 +149,6 @@ public static void main(String args[]) {
153149 // [7(rear), 2(front), 5, 3]
154150
155151 System .out .println (myQueue .peekFront ()); // Will print 2
156- System .out .println (myQueue .peekRear ()); // Will print 7
152+ System .out .println (myQueue .peekRear ()); // Will print 7
157153 }
158154}
You can’t perform that action at this time.
0 commit comments