Skip to content

Added Ring Queue(=Circular Queue) - #322

Closed
YooSeonjae wants to merge 1 commit into
TheAlgorithms:masterfrom
YooSeonjae:master
Closed

YooSeonjae wants to merge 1 commit into
TheAlgorithms:masterfrom
YooSeonjae:master

Conversation

@YooSeonjae

Copy link
Copy Markdown
Contributor

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.

@YooSeonjae YooSeonjae mentioned this pull request Dec 17, 2017

@christianbender christianbender left a comment

Copy link
Copy Markdown

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;

Copy link
Copy Markdown

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;

Copy link
Copy Markdown

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown

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)

Copy link
Copy Markdown

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)

Copy link
Copy Markdown

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use curly braces, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants