Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions DataStructures/Queues/Queues.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public Queue(int size) {
public boolean insert(int x) {
if (isFull())
return false;
if (rear == maxSize - 1) // If the back of the queue is the end of the array wrap around to the front
rear = -1;
rear++;
rear = (rear + 1) % maxSize; // If the back of the queue is the end of the array wrap around to the front
queueArray[rear] = x;
nItems++;
return true;
Expand All @@ -72,9 +70,7 @@ public int remove() { // Remove an element from the front of the queue
return -1;
}
int temp = queueArray[front];
front++;
if (front == maxSize) //Dealing with wrap-around again
front = 0;
front = (front + 1) % maxSize;
nItems--;
return temp;
}
Expand Down Expand Up @@ -153,6 +149,6 @@ public static void main(String args[]) {
// [7(rear), 2(front), 5, 3]

System.out.println(myQueue.peekFront()); // Will print 2
System.out.println(myQueue.peekRear()); // Will print 7
System.out.println(myQueue.peekRear()); // Will print 7
}
}