-
Notifications
You must be signed in to change notification settings - Fork 20k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Work! I have some requests.
// Create an array of size n | ||
objArray = new Object[n]; | ||
size = 0; | ||
front = 0; |
There was a problem hiding this comment.
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
.
objArray = new Object[n]; | ||
size = 0; | ||
front = 0; | ||
rear = 0; |
There was a problem hiding this comment.
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.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use curly braces. See here for if-else: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html#449
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) |
There was a problem hiding this comment.
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
// if the queue is empty throw new IllegalStateException ("The queue is empty"); | ||
Object object = new Object(); | ||
|
||
if (size == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use curly braces, too.
if (objArray == null) | ||
return sb.toString(); | ||
|
||
if (front == rear) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use curly braces, too.
public String toString() { | ||
StringBuffer sb = new StringBuffer(); | ||
|
||
if (objArray == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use curly braces, too.
circular queue or ring queue is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.
When you show a queue with an array using the Ring Queue, it shows that when you call remove (), there is no need to move a lot because of its free space.