/**
  * Adds the item
  * @param item the item to add
  */
 public void enqueue(Item item) {
     if (item == null) 
         throw new NullPointerException("null pointer not allowed");
     if (size == a.length) 
         resize(2 * a.length); // double size of array
     a[size++] = item;
 }
Example #2
0
  /**
   * Add the item
   *
   * @param item the item
   */
  public void enqueue(Item item) {
    if (item == null) throw new java.lang.NullPointerException();

    // double the size of the array if necessary
    if (N == a.length) resize(2 * a.length);

    a[N++] = item;
  }
 /**
  * add the item
  *
  * @param item the item
  */
 public void enqueue(Item item) {
   if (item == null) {
     throw new NullPointerException("Item cannot be null.");
   }
   if (size == queue.length) {
     resize(2 * size);
   }
   queue[size++] = item;
 }
Example #4
0
 // return the number of items on the queue
 public void enqueue(Item item) // add the item
     {
   if (item == null) throw new java.lang.NullPointerException();
   if (size == arr.length) {
     resize(size * 2);
   }
   arr[size] = item;
   ++size;
 }
Example #5
0
 /** Add the item. Double size of array if necessary */
 public void enqueue(Item item) {
   if (item == null) {
     throw new java.lang.NullPointerException();
   }
   if (this.size == a.length) {
     resize(2 * a.length);
   }
   a[this.size++] = item;
 }
 /**
  * Deletes and returns a random item on this queue.
  *
  * @return a random item on this queue
  * @throw java.util.NoSuchElementException if this queue is empty
  */
 public Item dequeue() {
   if (isEmpty()) throw new NoSuchElementException("Queue underflow");
   int rand = (int) (StdRandom.uniform() * N);
   Item item = a[rand];
   a[rand] = a[N - 1];
   a[N - 1] = null;
   N--;
   if (N > 0 && N == a.length / 4) resize(a.length / 2);
   return item;
 }
 /**
  * remove and return a random item
  *
  * @return a random item
  */
 public Item dequeue() {
   if (size() == 0) {
     throw new NoSuchElementException("Dequeue item from empty queue.");
   }
   int randomIndex = getRandomIndex();
   Item result = queue[randomIndex];
   queue[randomIndex] = queue[size - 1];
   queue[size - 1] = null;
   size--;
   if (size > 0 && size == queue.length / 4) {
     resize(queue.length / 2);
   }
   return result;
 }
    /**
     * Remove and return a random item
     * @return the removed item
     */ 
    public Item dequeue() {
        if (isEmpty())
            throw new NoSuchElementException("Queue underflow");

        int index = StdRandom.uniform(size);
        Item item = a[index];

        a[index] = a[size - 1];
        a[size - 1] = null;
        size--;
        // shrink size of array if neccessary
        if (size > 0 && size == a.length/4) resize(a.length / 2);
        return item;
    }
Example #9
0
 public Item dequeue() {
   if (size == 0) throw new java.util.NoSuchElementException();
   // StdRandom.shuffle(arr, 0, size- 1);
   int randomN = StdRandom.uniform(size);
   assert (size > 0);
   Item item = arr[randomN];
   swap(randomN, size - 1);
   arr[size - 1] = null;
   size--;
   if (size > 0 && size == arr.length / 4) {
     resize((arr.length) / 2);
   }
   return item;
 }
Example #10
0
 /** remove and return a random item */
 public Item dequeue() {
   if (isEmpty()) {
     throw new java.util.NoSuchElementException();
   }
   int index = StdRandom.uniform(this.size);
   this.size--;
   exchange(index, this.size);
   Item item = this.a[this.size];
   this.a[this.size] = null;
   if (this.size > 0 && this.size == this.a.length / 4) {
     resize(this.a.length / 2);
   }
   return item;
 }
Example #11
0
  /**
   * Remove and return a random item
   *
   * @return the random item
   */
  public Item dequeue() {
    if (N == 0) throw new java.util.NoSuchElementException();

    int i = StdRandom.uniform(N);

    exch(i, N - 1);

    Item item = a[N - 1];
    a[N - 1] = null; // don't loiter
    N--;

    if (N > 0 && N == a.length / 4) resize(a.length / 2);

    return item;
  }
  public Item dequeue() // delete and return a random item
      {
    if (isEmpty()) throw new NoSuchElementException("Queue underflow");

    //    System.out.println("\t(dequeue) head="+head+"; "+"tail="+tail+"; N="+N+";
    // capacity="+capacity());

    Item value = a[head];
    a[head] = null;
    head++;
    head %= a.length;
    N--;

    if (N > 0 && N == a.length / 4) resize(a.length / 2);

    return value;
  }
  public void enqueue(Item item) // add the item
      {
    if (item == null) throw new NullPointerException();

    //    System.out.println("\t(enqueue="+item+") head="+head+"; "+"tail="+tail+"; N="+N+";
    // capacity="+capacity());

    a[tail] = item;

    if (N > 0) {
      int i = StdRandom.uniform(N + 1);
      exchange(tail, (head + i) % a.length);
    }

    tail++;
    tail %= a.length;
    N++;

    if (N == a.length) resize(2 * a.length);
  }
 /**
  * Adds the item to this queue.
  *
  * @param item the item to add to this queue
  * @throw java.util.NullPointerException if item is null
  */
 public void enqueue(Item item) {
   if (item == null) throw new NullPointerException("Cannot add null item");
   if (N == a.length) resize(2 * a.length);
   a[N++] = item;
 }
Example #15
0
 /** Construct an empty randomized queue */
 public RandomizedQueue() {
   N = 0;
   resize(2);
 }