My question has to deal with the end part of my pseudo code.

Basically the method I'm doing is taking the front name in the queue and moving it to the rear and making the next in line to be the new front in the queue, he recomended using E poll or was it E peek? to get the item in the front and then something that started with a g at the very end of the method to hold the new front queue I believe. Does anyone know what that G word would be by anychance? I guess I don't know how to explain the last part. Sorry if this doesn't make sense. Thanks again for the help.

Also your suppose to do nothing if queue is empty.

so:

public void moveToRear(E item){
if (front ==null);
return;
E poll();
//this is the part I need help with
gwordsomething(item);

So ya do you guys get what I'm asking when it comes to the gwordsomething(item);

This was something that I was coming up with as an alternative based upon the code were adding too, still a work in progress.

public void moveToRear(){
E item = peek();
if (front==null);
return;
else{
      rear.next = front;
      rear = rear.next;
      front =  next.item;
    }
 }

So like I said a work in progress lol.

Here is the code we are adding too

import java.util.*;

/** Implements the Queue interface using a single-linked list.
*   @author Koffman & Wolfgang
* */

public class ListQueue < E >
    extends AbstractQueue < E >
    implements Queue < E > {

  // Data Fields
  /** Reference to front of queue. */
  private Node < E > front;

  /** Reference to rear of queue. */
  private Node < E > rear;

  /** Size of queue. */
  private int size;

  /** A Node is the building block for a single-linked list. */
  private static class Node < E > {
    // Data Fields
    /** The reference to the data. */
    private E data;

    /** The reference to the next node. */
    private Node next;

    // Constructors
    /** Creates a new node with a null next field.
        @param dataItem The data stored
     */
    private Node(E dataItem) {
      data = dataItem;
      next = null;
    }

    /** Creates a new node that references another node.
        @param dataItem The data stored
        @param nodeRef The node referenced by new node
     */
    private Node(E dataItem, Node < E > nodeRef) {
      data = dataItem;
      next = nodeRef;
    }
  } //end class Node

  // Methods
  /** Insert an item at the rear of the queue.
      post: item is added to the rear of the queue.
      @param item The element to add
      @return true (always successful)
   */
  public boolean offer(E item) {
    // Check for empty queue.
    if (front == null) {
      rear = new Node < E > (item);
      front = rear;
    }
    else {
      // Allocate a new node at end, store item in it, and
      // link it to old end of queue.
      rear.next = new Node < E > (item);
      rear = rear.next;
    }
    size++;
    return true;
  }

  /** Remove the entry at the front of the queue and return it
      if the queue is not empty.
      post: front references item that was second in the queue.
      @return The item removed if successful, or null if not
   */
  public E poll() {
    E item = peek(); // Retrieve item at front.
    if (item == null)
      return null;
    // Remove item at front.
    front = front.next;
    size--;
    return item; // Return data at front of queue.
  }

  /** Return the item at the front of the queue without removing it.
      @return The item at the front of the queue if successful;
              return null if the queue is empty
   */
  public E peek() {
    if (size == 0)
      return null;
    else
      return front.data;
  }

  public boolean add(E item) {
    return offer(item);
  }

  public E remove() {
    if (isEmpty())
      throw new NoSuchElementException();
    return poll();
  }

  public E element() {
    if (isEmpty())
      throw new NoSuchElementException();
    return peek();
  }

  public int size() {
    return size;
  }

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

  public Iterator < E > iterator() {
    return new Iter();
  }

  private class Iter
      implements Iterator < E > {
    Node < E > next = front;

    public boolean hasNext() {
      return next != null;
    }

    public E next() {
      E item = next.data;
      next = next.next;
      return item;
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }
  }
}

Maybe I'm thinking get(item) but I don't think that would work at all to begin with.

I'm not sure what the "gwordsomething" should be, but my pseudocode for your would go something like this:

if list isEmpty - return
E item = list.remove
list.add( item )

Now, take a look at those methods I have referenced and you will see that this should work. First, we check if the list is empty. If not, we remove the first element in the list and add it back in at the end.

Also, is there an error in the code you have to add to? There appears to be two methods called remove() which is invalid since the take the same parameters (none).

EDIT: Ignore this, one of the methods is in an inner class (my mistake, sorry :$)

I'm not sure what the "gwordsomething" should be, but my pseudocode for your would go something like this:

if list isEmpty - return
E item = list.remove
list.add( item )

Now, take a look at those methods I have referenced and you will see that this should work. First, we check if the list is empty. If not, we remove the first element in the list and add it back in at the end.

I know what it is its offer its not a gsomething word its offer().

So it should be like this I believe(pseudo code)

So basically First check to see if the list is empty, if it is just 'return', if
it isnt, 'poll' then 'offer', and that should be it

E item = poll()

offer()

So something like this:

public void moveToRear(){
    if(front==null);
    return;
    else{
      E item = poll();
      offer(item);
    }
  }

Yes your pseudocode will work, but may I make a suggestion? My pseudocode would be:

if ( isEmpty() ) {
  return;
}
E item = remove();
add( item );

Now, both your code and mine will do the same thing, but mine will be easier for someone else to read. You may argue that all that the remove method does is call poll(), but just by looking at the moveToRear method, which one is easier to work out what is happening? Same goes for add and offer. But why did I say isEmpty rather than front == null? Again, I think it is clearer to say "is the list empty?" rather than "is the front Node null?" but that's just my personal opinion.

This article has been dead for over six months. Start a new discussion instead.