Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Queue_ArrayBased implements IQueue, IArrayBased {
- int size;
- Object []array;
- public Queue_ArrayBased(int size) {
- this.size = size;
- array = new Object[size];
- }
- int f = 0, r = 0;
- public void enqueue(Object item) {
- if (size() == size)
- throw new RuntimeException();
- array[r] = item;
- r = (r + 1) % size;
- }
- public Object dequeue() {
- if (isEmpty())
- throw new RuntimeException();
- Object getter = array[f];
- array[f] = null;
- f = (f + 1) % size;
- return getter;
- }
- public boolean isEmpty() {
- return (f == 0 && r == 0);
- }
- public int size() {
- return (size - f + r) % size;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement