2013-02-01 57 views
0

我搜索了这个许多发现alota类似的答案,但没有什么帮助我确切的问题。Java双链表堆栈尾错误

我正在为我的双链表执行一个推式方法,而头上的指针正常工作,尾指针和先前指针不起作用请帮忙。

public class MyStack<E> implements MyDeque { 

    private Node<E> head; 
    private Node<E> tail; 
    private int size; 

    public MyStack() { 
     head = null; 
     tail = null; 
     size = 0; 
    } 
    public void push(Object element) { 
     Node<E> newNode = new Node(element); 
     if(size == 0) { 
      Node temp = new Node(head); 
      head = newNode; 
      head.next = head; 
      head.previous = head; 
      tail = head; 
      tail.next = head; 
      tail.previous = temp;   

      } 
     else {  

      newNode.previous = head; 
      head = newNode; 
      newNode.next = tail; 
      (tail.next).previous = tail; 


     }//else statement 
     size++; 
    }//push() 


    public Object peek() { 
     if (size==0) return null; 
     else 
      return head; 
    } 


    public Object pop() { 

     size--; 
     if(size == 0) 
      return null; 
     else { 

     Node temp = new Node(head.previous); 
     head = head.previous; 
     head.next = tail; 
     head.previous = temp; 

     return head; 

     }//else 

    }//pop() 

    @Override 
    public int size() { 

     return size; 
    } 


    private class Node<E> { 
     private E data; 
     private Node<E> next; 
     private Node<E> previous; 

     public Node(E data) { 
      this.data = data; 
      this.next = null; 
      this.previous = null; 
     } 
     public Node(E data, Node<E> next, Node<E> previous) { 

      this.data = data; 
      this.next = next; 
      this.previous = previous; 

     }//constructor 

     public String toString() { 
      return data+""; 
     } 

    }//class Node<E> 
    public String toString() { 

     return (head+" Head\n" + head.next +" Head.Next\n" + head.previous+ " Head.previous\n" 
       + tail+" Tail\n" + tail.next+" tail.next\n" + tail.previous+" tail.previous\n"); 

    } 


} 
+0

堆栈不需要双链表,只需要一个指向头部的指针和Node中的下一个变量。看起来你有一个环绕式堆栈(头指向尾部,反之亦然),我的问题是 - 为什么?在我看来,在'push'中为'size == 0',除了其他变化之外,所有4个'next'和'previous'都应该是'null'。 – Dukeling

回答

0

在堆栈为空时推送对象的情况看起来不正确。添加的第一个节点应该分配给头部。然后尾部变成新节点的头部,这个新节点变成尾节点的尾部,新节点本身变成尾部。

通常在堆栈中添加和删除元素(尾部)。该代码没有说明你正在试图用成员做什么。也许它会更清晰,如果你给他们起名firstNodelastNode

0

我可以清楚地看到这个代码的几个问题

public class MyStack<E> implements MyDeque { 

private Node<E> head; 
private Node<E> tail; 
private int size; 

public MyStack() { 
    head = null; 
    tail = null; 
    size = 0; 
} 

这看上去很好

public void push(Object element) { 
    Node<E> newNode = new Node(element); 
    if(size == 0) { 
     Node temp = new Node(head); 
     head = newNode; 
     head.next = head; 
     head.previous = head; 
     tail = head; 
     tail.next = head; 
     tail.previous = temp;   

     } 
    else {  

     newNode.previous = head; 
     head = newNode; 
     newNode.next = tail; 
     (tail.next).previous = tail; 


    }//else statement 
    size++; 
}//push() 

有点糊涂了,为什么你在一个对象,而不是你的通用E.

服用你真的不需要这里的临时价值,事实上,当你设置tail.previous = temp

在你的其他地方你没有正确设置tail.previous。最后一行应该是tail.previous = head。你也错过了head.next

所以清理了一下

public void push(E element) { 
    Node newNode = new Node(E); 
    if(size == 0) { 
     head = newNode; 
     head.next = head; 
     head.previous = head; 
     tail = head; 
    } else { 
     newNode.previous = head; 
     head.next = newNode; 
     newNode.next = tail; 
     tail.previous = newNode; 
     head = newNode; 
    } 
    size++; 
} 

在你可能想你的尺寸检查后,将你的大小递减的流行方法,否则弹出当1个元素堆栈将返回null 。

编辑:这可能会更容易与单链表,或者至少不是一个循环的双向链表。

并在最后添加可能是更常规的方式来做事情。