1
\$\begingroup\$

here is my LinkedList Queue Implementation , seems like its almost like the linkedlist implementation with little differences mainly in the poll() method, i think that the offer() method is same like the linkedlist method add() , the point is that in the offer() method we are adding the elements to the tail, than we are moving them to the left, but their next is same like the add() method, where we are adding in the head and moving the elements to the right... Is i'm right? I'm glad to hear any suggestations...

import java.util.NoSuchElementException;

public class DynamicQueue {

private class Node {
    private Object item;
    private Node next;

    Node(Object item){
        this.item = item;
        this.next = null;
    }

    Node(Object item, Node prevNode){
        this.item = item;
        prevNode.next = this;
    }
}

private Node head;
private Node tail;
private int count;

public DynamicQueue() {
    this.head = null;
    this.tail = null;
    this.count = 0;
}

public int size() {
    return count;
}

public void offer(Object item) {
    if(tail == null) {
        tail = new Node(item);
        head = tail;
    }else {
        Node newNode = new Node(item,tail);
        tail = newNode;
    }
    count++;
}

public void clear() {
    head = null;
}

public Object peek() {
    if(head == null) {
        throw new NoSuchElementException("Overflow Exception");
    }else {
        return head.item;
    }
}

public void poll() {
    if(head == null) {
        throw new NoSuchElementException("Overflow Exception");
    }
    Node currentNode = head.next;
    head.next = null;
    head = currentNode;
    count--;

}

public static void main(String[] args) {
    DynamicQueue queue = new DynamicQueue();
    queue.offer("First");
    queue.offer("Second");
    queue.offer("Third");
    queue.offer("Fourth");
    queue.poll();
    System.out.println(queue.peek());
    queue.poll();
    System.out.println(queue.peek());
    queue.poll();
    System.out.println(queue.peek());

}

}
\$\endgroup\$
1
  • \$\begingroup\$ I have rolled back your last edit. Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$
    – Heslacher
    Commented Oct 1, 2018 at 15:33

1 Answer 1

1
\$\begingroup\$

clear() is buggy

The following code causes a NoSuchElementException:

DynamicQueue queue = new DynamicQueue();
queue.offer("First");
queue.clear();
queue.offer("First");
queue.poll();

You can ealy fix it by adding tail = null; to your clear() method.

Use selfexplaing exception messages

An exception name (in this case NoSuchElementException) already describes the kind of error accurred. The cause should explain why this happened. A better message for exaple could simply be "this queue is empty".

Suggestion: add a return type to poll()

If poll() returns the current head, the user doesn't need to do call peek(). This wouldn't even change your main(String[]), since the user is not forced to use the returned object at all.


Of course, you could also add a generic type to your class instead of using Object inside your Node, but I guess that was not your intension, since you are already familiar with the Java Collections.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Thanks for the the suggestions. About the clear() method your right, but i think it need to be head = tail = null, because if we added two objects with the offer() method, than if we use a clear() method and than poll(), pull need to throw a Exception, but with tail = null it works after the clear method(). About the return statement, i check the methods of the Queue and it doesn't have a return statement in the poll() method like the Stack, because of that i didn't added... \$\endgroup\$
    – E.Etem
    Commented Oct 1, 2018 at 13:50
  • \$\begingroup\$ @E.Etem java.util.Queue<E>.poll() actually has a return type: "the head of this queue, or null if this queue is empty". source: docs.oracle.com/javase/7/docs/api/java/util/Queue.html \$\endgroup\$
    – saucecode
    Commented Oct 1, 2018 at 13:58
  • \$\begingroup\$ sorry seems like the book i'm reading have a error. \$\endgroup\$
    – E.Etem
    Commented Oct 1, 2018 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.