Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have written these codes and in trying out the code in my main method, it looks like I am on the right way, but which disturbs me is the fact, that whenever I say:

while (!empty(){

it give me front of the queue. It seems like the queue never gets empty and the loop is running forever. Is this the logic of circular doubly linked lists, that it never gets empty? I think I might be making mistakes in the dequeue method.

package übung;

import java.util.NoSuchElementException;

public class QueueCircularDLL<T> implements ADTQueue<T> {
private ListElement<T> head;
private ListElement<T> tail;

public QueueCircularDLL(){
    head=null;
    tail=null;
}

public void enq (T element){
    ListElement<T>newElement=new ListElement<T>(element);
    if (empty()){
        head= newElement;
        tail= head;
    }

    tail.setNextElement(newElement);
    newElement.setPrevElement(tail);
    tail=tail.getNextElement();
    tail.setNextElement(head);
    head.setPrevElement(tail);

}

    public void deq(){
    if (empty()){
        throw new RuntimeException("queue is empty");
    }

    if (head==tail){
        //head=null;
        head=null;
        tail=null;

    }
    else{
    head=head.getNextElement();
    head.setPrevElement(tail);
    tail.setNextElement(head);
    }

}

public T front(){
    if(empty()){
        return null;
    }
    return head.getElement();
}

public boolean empty(){
    return (head==null);
}

private static class ListElement<T>{
    private T element = null;
    private ListElement<T> nextElement = null;
    private ListElement<T> prevElement = null;

    public ListElement(T element) {
        this.element = element;
    }

    public T getElement() {
        return element;
    }

    public ListElement<T> getNextElement() {
        return nextElement;
    }

    public void setNextElement(ListElement<T> element) {
        this.nextElement = element;
    }
    //für zirkuläre doppelt verkettete liste
    public ListElement<T>getPrevElement(){
        return prevElement;
    }
    // für zirkuläre doppelt verkette liste
    public void setPrevElement(ListElement<T> element){
        this.prevElement=element;
    }



}

public static void main(String[] args) {
    QueueCircularDLL<Integer> queue = new QueueCircularDLL<Integer>();

    for (int i = 0; i < 10; ++i) {
        queue.enq(i);
    }

    //while (!queue.empty()) {
    for(int i=0; i<20; i++){
        System.out.println(queue.front());
        queue.deq();
    }
}


}
share|improve this question
1  
Hi. Welcome to Code Review! We don't do code debugging, so this does not really fit our site. After you fix the code so that it works, we could help you. Note that the problem seems to be that you update head and tail when you enqueue but only half update head when you dequeue. If you update tail properly, then you won't need to check the null case except to gate the update of the previous element for the new head. Hint: after you update head, it's the same two commands to update tail and head as you use in enqueue. –  mdfst13 Jul 16 at 18:21
    
sry, but I didnt get it :( I tried it out, but seems like I get the same problems... –  user3683131 Jul 16 at 20:36
    
this is, how I did it now, but still have the same problem... head=head.getNextElement(); head.setPrevElement(tail); tail.setNextElement(head); if (head.getPrevElement()==null){ tail = null; } –  user3683131 Jul 16 at 21:15

1 Answer 1

I have edited my post and replaced the deque method with following codes:

    public void deq(){
    if (empty()){
        throw new RuntimeException("queue is empty");
    }

    if (head==tail){
        //head=null;
        head=null;
        tail=null;

    }
    else{
    head=head.getNextElement();
    head.setPrevElement(tail);
    tail.setNextElement(head);
    }

}

it looks like it is working now, I still get 0-9 without any repetitions and errors... is this the right way? I guess it can also be improved, right?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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