Skip to content

Added Ring Queue(=Circular Queue) #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
94 changes: 94 additions & 0 deletions Data Structures/Queues/RingQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
public interface Queue {

public void add(Object object);

public Object first();

public Object remove();

public int size();

}
public class RingQueue implements Queue {
private Object[] objArray = null;
private int size;
private int front;
private int rear;

public RingQueue(int n) {
// Create an array of size n
objArray = new Object[n];
size = 0;
front = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that for (front+1) % objArray.length the pointer front never reaches the element with index 0 of the array. I would front set equal to -1.

rear = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing as above.

}

@Override
public void add(Object obj) {
// type obj if there is empty space in the queue
// if there is no empty space in the queue throw new IllegalStateException ("The queue is full");
if (size == objArray.length)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new IllegalStateException("The queue is full");

else {
rear = (rear + 1) % objArray.length;
objArray[rear] = obj;
size++;
}

}

@Override
public Object first() {
// return the first element if the queue is not empty
// if the queue is empty throw new IllegalStateException ("The queue is empty");
if (size == 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing. Use curly braces

throw new IllegalStateException("The queue is empty");

return objArray[(front + 1) % objArray.length];
}

@Override
public Object remove() {
// If the queue is not empty, delete the first element and return
// if the queue is empty throw new IllegalStateException ("The queue is empty");
Object object = new Object();

if (size == 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use curly braces, too.

throw new IllegalStateException("The queue is empty");

else {
front = (front + 1) % objArray.length;
object = objArray[front];

size--;
}
return object;
}

@Override
public int size() {
// returns the number of elements stored in the queue
return size;
}

@Override
public String toString() {
StringBuffer sb = new StringBuffer();

if (objArray == null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use curly braces, too.

return sb.toString();

if (front == rear)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use curly braces, too.

return sb.toString();

int tempFront = front;
do {
tempFront = (tempFront + 1) % objArray.length;
sb.append(objArray[tempFront]);
} while (tempFront != rear);

return sb.toString();
}

}