HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Queue over resizable array implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
resizablearrayimplementationqueueover

Problem

I study data structures on coursera's course, and there is an extra exercise to create a Queue data structure.

I created it:

class Queue {

  Integer[] data;
  int head, tail;

  public Queue() {
    data = new Integer[2];
    head = 0;
    tail = 0;
  }

  public int size() {
    return tail - head;
  }

  public boolean isEmpty() {
    return size() == 0;
  }

  private void realign() {
    java.util.Arrays.sort(data, new java.util.Comparator() {
      public int compare(Object o1, Object o2) {
        if (o1 == null) return 1;
        else if(o2 == null) return -1;
        else return 0;
      }
    });
    tail -= head;
    head = 0;
  }

  private void resize() {
    if (tail == data.length && size() != data.length) {
      realign();
    }
    if (data.length == size()) {
      int newLength = data.length * 2;
      //Duplication
      Integer[] newData = new Integer[newLength];
      System.arraycopy(data, 0, newData, 0, data.length);
      data = newData;
    } else if (size() == data.length / 4 && size() != 0) {
      int newLength = data.length / 2;
      //Duplication
      Integer[] newData = new Integer[newLength];
      System.arraycopy(data, 0, newData, 0, data.length);
      data = newData;
    }
  }

  public void enqueue(Integer item) {
    if (item == null) { throw new NullPointerException(); }
    resize();
    data[tail] = item;
    tail++;
    return;
  }

  public int dequeue() {
    if (isEmpty()) { throw new java.util.NoSuchElementException(); }
    int res = data[head];
    data[head] = null;
    head++;   
    if (head == tail) {
      head = tail = 0;
    }
    return res;
  }
}


And a test suit for it:

```
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.*;

public class QueueTest {

@Test
public void testQueueN() {
Queue q = new Queue();
int N = 65;
for(int i = 0; i < N; i++) {
q.enqueue(i);
}
assertFalse(q.isEmpty());
for(int i = 0; i < N; i++) {
assertEquals(i

Solution

Imports:

import java.util.*;


NEVER do this. Look how many classes you got now. This is ineffective and needlessly clutters your IDE's "do what I want button"-suggestions.

The fun thing is, in your main class you do it the other way round:

java.util.Arrays.sort(data, new java.util.Comparator() {


why not:

import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

//Later:
Arrays.sort(data, new Comparator()) {


This is shorter, does exactly the same and allows your ClassLoader to preload the classes needed --> possible performance improvements
Queue and Scoping

While we're in the java.util package.. You can find an interface there.. Queue. This is something you can program against. To prevent a name-conflict I'd rename your implementation to MyQueue or something similar.

You class would then begin with the following:

public class MyQueue implements Queue {
    private Integer[] data;
    private int head;
    private int tail;


Here I also made some changes to your code.

Why? In short: You should restrict the visibility of members as much as possible. With your code, classes in the same package as your Queue could do:

Queue queue = new Queue(); 
queue.data[0] = Integer.MAX_VALUE;


This is not good. noone should be messing with the state of your class. That's why these members should be private (or maximally protected), but definitely not package-private which is the default scope.

same goes for your head and tail.

Code Snippets

import java.util.*;
java.util.Arrays.sort(data, new java.util.Comparator<Object>() {
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

//Later:
Arrays.sort(data, new Comparator<Object>()) {
public class MyQueue implements Queue<Integer> {
    private Integer[] data;
    private int head;
    private int tail;
Queue queue = new Queue(); 
queue.data[0] = Integer.MAX_VALUE;

Context

StackExchange Code Review Q#56193, answer score: 4

Revisions (0)

No revisions yet.