在Java中出局

时间:2015-08-09 13:44:45

标签: java deque

我在完全实现enqueue和dequeue部分时遇到了麻烦。这是我想要得到的结果:

DEQUE TESTING

The size of the deque is: 3

The deque contains:

4 9 8 

4

8

9

1

11

The size of the deque is: 2

The deque contains:


11 1

但这就是我所得到的:

DEQUE TESTING
The size of the deque is: 3
The deque contains:
 4 9 8
4
null
null
null
null
The size of the deque is: 0
The deque contains:

所以,它只打印到某一点。为了解决这个问题,我已经多次(实际上很多次)完成了我的代码,但我无法确定问题所在。我觉得它有些微不足道的东西需要改变。

这是我的代码:

public class Murray_A06Q3 {
    public static void main(String[] args) {

        LinkedDeque<Integer> deque = new LinkedDeque<Integer>();

        System.out.println("DEQUE TESTING");

        deque.enqueueBack(3);
        deque.enqueueBack(7);
        deque.enqueueBack(4);
        deque.dequeueFront();        
        deque.enqueueBack(9);
        deque.enqueueBack(8);
        deque.dequeueFront();

        System.out.println("The size of the deque is: " + deque.size());
        System.out.println("The deque contains:\n" + deque.toString());   

        System.out.println(deque.dequeueFront());        

        deque.enqueueFront(1);
        deque.enqueueFront(11);                         
        deque.enqueueFront(3);                 
        deque.enqueueFront(5);         

        System.out.println(deque.dequeueBack());
        System.out.println(deque.dequeueBack());        
        System.out.println(deque.last());                

        deque.dequeueFront();
        deque.dequeueFront();        

        System.out.println(deque.first());        
        System.out.println("The size of the deque is: " + deque.size());
        System.out.println("The deque contains:\n" + deque.toString());            

    } // End of main method

public static class LinkedDeque<T> implements DequeADT<T> {

    private int count; 
    private LinearDoubleNode<T> firstNode, lastNode;

// constructor
    public LinkedDeque(){

        count = 0;
        firstNode = null;
        lastNode = null;

    } // end of constructor

// Beginning of enqueueFront
    public void enqueueFront(T element) {
        LinearDoubleNode newNode = new LinearDoubleNode();

        if(isEmpty()){
            lastNode = newNode;
            count++;
        }
        else
            firstNode.setPrev(newNode);
        firstNode = newNode;
        count--;

    } // end of enqueFront

// Beginning of enqueueBack
    public void enqueueBack(T element) {
        LinearDoubleNode<T> node = new LinearDoubleNode<T>(element);

        if (isEmpty())
           firstNode = node;
        else
            lastNode.setNext(node);

        lastNode = node;
        count++;

    } // end of enqueueBack

// Beginning of dequeueFront
    public T dequeueFront() {

        T front = null;
        if (!isEmpty()) {
            front = firstNode.getElement();
            firstNode = firstNode.getNext();
            count--;

            if (firstNode == null) {
                lastNode = null;
            }
            else 
                firstNode.setPrev(firstNode);
        }
        return front;

    } // end of dequeueFront

// Beginning of dequeueBack
    public T dequeueBack() {

        T back = null;
        if (!isEmpty()) {
            back = lastNode.getElement();
            lastNode = lastNode.getPrev();

            if (lastNode == null) {
                firstNode = null;
            }
            else 
                lastNode.setNext(null);
        }
        return back;

    } // end of dequeueBack()

    public T first() {
        return firstNode.getElement();
    }

    public T last() {
        return lastNode.getElement();
    }

// Beginning of isEmpty()           
    public boolean isEmpty() {

        if (count == 0) {
            return true;
        }       
        else 
            return false;

    } // End of isEmpty()

// Beginning of size()          
    public int size() {
        return count;
    }

 // Begin of toString() method          
    public String toString() {

        if (isEmpty()) {
            return " ";
        }

        StringBuilder sb = new StringBuilder();
        LinearDoubleNode<T> next = firstNode;

        while(next != null){
            sb.append(" ").append(next.getElement());
            next = next.getNext();
        }

        return sb.toString();

    } // End of toString()

  } // End of LinkedDeque

} // End of class header

2 个答案:

答案 0 :(得分:1)

您忘记设置上一个和元素。在递增计数时,您也遇到了一些错误。关于一些未抛出的异常,请注意。尽管如此,它现在应该是直截了当的。您的工作代码低于所需的输出:

  public static class LinkedDeque<T> {

        private int count;
        private LinearDoubleNode<T> firstNode, lastNode;

        // constructor
        public LinkedDeque(){

            count = 0;
            firstNode = null;
            lastNode = null;

        } // end of constructor

        // Beginning of enqueueFront
        public void enqueueFront(T element) {
            LinearDoubleNode newNode = new LinearDoubleNode();
            newNode.setElement(element);
            if(isEmpty()){

                lastNode = newNode;
                firstNode = newNode;
            }
            else {
                LinearDoubleNode second=firstNode;
                firstNode=newNode;
                firstNode.setNext(second);
                second.setPrev(firstNode);
               // firstNode.setPrev(newNode);

            }

            count++;

        } // end of enqueFront

        // Beginning of enqueueBack
        public void enqueueBack(T element) {
            if (element==null) throw  new NullPointerException("cannot add null to the list");

            LinearDoubleNode<T> node = new LinearDoubleNode<T>(element);
            node.setElement(element);
            if (isEmpty()){
                firstNode = node;
                lastNode=node;}
            else{
                LinearDoubleNode<T> before=lastNode;
                lastNode=node;
                before.setNext(lastNode);
                lastNode.setPrev(before);
            }


            count++;

        } // end of enqueueBack

        // Beginning of dequeueFront
        public T dequeueFront() {

            T front = null;
            if (count==1){
                front=firstNode.getElement();
                firstNode=null;
                lastNode=null;
                count--;
            }
            else if (!isEmpty()) {
                count--;
                front = firstNode.getElement();
                firstNode = firstNode.getNext();
            }

            return front;

        } // end of dequeueFront

        // Beginning of dequeueBack
        public T dequeueBack() {

            T back = null;
            if (count==1){
                back = lastNode.getElement();
                lastNode = null;
                firstNode = null;
                count--;
            }
             else if (!isEmpty()) {
                count--;
                back = lastNode.getElement();
                lastNode = lastNode.getPrev();
                lastNode.setNext(null);
            }
            return back;

        } // end of dequeueBack()

        public T first() {
            return firstNode.getElement();
        }

        public T last() {
            return lastNode.getElement();
        }

        // Beginning of isEmpty()
        public boolean isEmpty() {

            return count==0;

        } // End of isEmpty()

        // Beginning of size()
        public int size() {
            return count;
        }

        // Begin of toString() method
        public String toString() {

            if (isEmpty()) {
                return " ";
            }

            StringBuilder sb = new StringBuilder();
            LinearDoubleNode<T> next = firstNode;

            while(next != null){
                sb.append(" ").append(next.getElement());
                next = next.getNext();
            }

            return sb.toString();

        } // End of toString()

    } // End of LinkedDeque

} // End of class header

    DEQUE TESTING
The size of the deque is: 3
The deque contains:
 4 9 8
4
8
9
1
11
The size of the deque is: 2
The deque contains:
 11 1

答案 1 :(得分:0)

双向链表的第一条规则是在添加元素时必须维护两个链接。这是你的代码:

public void enqueueFront(T element) {
    LinearDoubleNode newNode = new LinearDoubleNode();

    if(isEmpty()){
        lastNode = newNode;
        count++;
    }
    else
        firstNode.setPrev(newNode);
    firstNode = newNode;
    count--;

} // end of enqueFront

首先,您永远不会将元素的值放入新节点。

其次,队列应该看起来像

A ⇆ B ⇆ C

但是,让我们看看你如何添加一个&#34; A&#34;当你只有一个&#34; B&#34;在队列中。您的firstNode是&#34; B&#34;,并将其prev设置为setPrev以指向您的&#34; A&#34;。

A ← B

但是你没有从新的&#34; A&#34;到&#34; B&#34;。这意味着当你需要从正面看队列时,它似乎只有一个元素 - A没有&#34; next&#34;!

您的else条款应为:

    else {
        firstNode.setPrev(newNode);
        newNode.setNext(firstNode);
    }

然后你就可以从双方遍历这份名单了。同样的逻辑也应该应用于enqueueBack

现在,你的出队逻辑:

public T dequeueFront() {

    T front = null;
    if (!isEmpty()) {
        front = firstNode.getElement();
        firstNode = firstNode.getNext();
        count--;

        if (firstNode == null) {
            lastNode = null;
        }
        else 
            firstNode.setPrev(firstNode);
    }
    return front;

}

现在,在您出列后,将新的firstNode设置为自身。这意味着您可能有一个无限循环。一旦你这样做了,如果你试着走&#34; prev&#34;通过从后面出列的链接,您将一次又一次地获得相同的节点。反向链接应设置为null(顺便说一下,您可以在dequeueBack中正确执行此操作。)