-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
rear = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use curly braces, too. |
||
return sb.toString(); | ||
|
||
if (front == rear) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
||
} |
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 pointerfront
never reaches the element with index 0 of the array. I wouldfront
set equal to-1
.