2013-03-01 64 views
0

計算機科學Java堆空間我編碼的程序,保持頭部的「尾巴」結束,節點,這樣我可以添加和節點/ LinkedList的刪除。這並不完全困難,但我遇到了一個讓我哭泣的問題。我正在運行到java堆空間(內存不足)。與LinkedLists和節點

這裏是我的代碼:

import java.util.NoSuchElementException; 

/** 
A linked list is a sequence of nodes with efficient 
element insertion and removal. This class 
contains a subset of the methods of the standard 
java.util.LinkedList class. 
*/ 
public class LinkedList 
{ 
    private Node head; 
    private int currentSize; 
    private Node tails; 
    /** 
    Constructs an empty linked list. 
    */ 
    public LinkedList() 
    { 
     head = null; 
     tails = null; 
     currentSize = 0; 
    } 

    /** 
    Adds an element to the front of the linked list. 
    @param element the element to add 
    */ 
    public void addFirst(Object element) 
    { 
     Node newNode = new Node(); 
     if(head == null){ 
      tails = newNode; 
     } 
     newNode.data = element; 
     currentSize++; 
     newNode.next = head; 
     head = newNode; 
    } 

    /** 
    Removes the head element in the linked list. 
    @return the removed element 
    */ 
    public Object removeFirst(){ 
     if (head == null) 
      throw new NoSuchElementException(); 
     Object temp = head.data; 
     head = head.next; 
     if(tails.next == null) 
      tails.next = head; 

     currentSize--; 
     return temp; 
    } 

    /** 
    Returns the head element in the linked list. 
    @return the head element in the linked list 
    */ 
    public Object getFirst() 
    { 
     if (head == null) 
      throw new NoSuchElementException(); 
     return head.data; 
    } 

    /** 
    Returns the element at a given position in the linked list. 
    @param index the element position 
    @return the element at the given position 
    */ 
    Object get(int index) 
    { 
     if (index < 0) 
      throw new NoSuchElementException(); 

     Node current = head; 
     int i = 0; 

     while (current != null && i < index) 
     { 
      current = current.next; 
      i++; 
     } 

     if (current == null) 
      throw new NoSuchElementException(); 
     return current.data; 
    } 

    /** 
    Computes the size of the linked list 
    @return the number of elements in the list 
    */ 
    public int size() 
    { 
     //to be completed for lab 7.1 #2 
     return currentSize; // rewrite this, just makes it compile 
    } 

    /** 
    Reverses all elements in a linked list. 
    */ 
    public void reverse(){ 
     Node currentNode, nextNode, loopNode; 
     if(head==null) 
      return; 
     currentNode=head; 
     nextNode= head.next; 
     loopNode = null; 
     while(nextNode != null){ 
      currentNode.next = loopNode; 
      loopNode= currentNode; 
      currentNode=nextNode; 
      nextNode =nextNode.next; 
     } 
     head = currentNode; 
     head.next = loopNode; 

} 


    /** 
    Adds an element to the end of the linked list. 
    @param element the element to add 
    */ 
    public void add(Object element){ 
     Node current = new Node(); 
     current.data = element; 
     if(tails.next == null) 
      tails.next = current; 

     tails = head; 
     currentSize ++; 
    }  

    /** 
    Returns an iterator for iterating through this list. 
    Allows the use of the iterator outside of this class. 
    @return an iterator for iterating through this list 
    */ 
    public ListIterator listIterator() 
    { 
     return new LinkedListIterator(); 
    } 

    /** 
    Returns a string representation of this list in brackets 
    separated by commas. 
    @return a string of list elements. 
    */ 
    public String toString() 
    { 
     StringBuilder temp = new StringBuilder(); 
     ListIterator it = listIterator(); 
     while(it.hasNext()){ 
      temp.append(it.next()); 
      if(it.hasNext()) 
       temp.append(", "); 
     } 
     return temp.toString();  
    } 

    private static class Node 
    { 
     private Object data; 
     private Node next; 


    } 

    private class LinkedListIterator implements ListIterator 
    {    
     private Node position; 
     private Node previous; 

     /** 
     Constructs an iterator that points to the front 
     of the linked list. 
     */ 
     public LinkedListIterator() 
     { 
      position = null; 
      previous = null; 

     } 

     /** 
     Moves the iterator past the next element. 
     @return the traversed element 
     */ 
     public Object next() 
     { 
      if (!hasNext()) 
       throw new NoSuchElementException(); 
      previous = position; // Remember for remove 

      if (position == null) 
       position = head; 
      else 
       position = position.next; 

      return position.data; 
     } 

     /** 
     Tests if there is an element after the iterator 
     position. 
     @return true if there is an element after the iterator 
     position 
     */ 
     public boolean hasNext() 
     { 
      if (position == null) 
       return head != null; 
      else 
       return position.next != null; 
     } 

     /** 
     Adds an element before the iterator position 
     and moves the iterator past the inserted element. 
     @param element the element to add 
     */ 
     public void add(Object element) 
     { 
      if (position == null) 
      { 
       addFirst(element); 
       position = head; 
       tails = head; 
      } 
      else{ 
       Node newNode = new Node(); 
       newNode.data = element; 
       newNode.next = position.next; 
       position.next = newNode; 
       position = newNode; 
       previous = position; 
       tails = newNode; 
      } 
       currentSize ++; 
     } 

     /** 
     Removes the last traversed element. This method may 
     only be called after a call to the next() method. 
     */ 
     public void remove() 
     { 
      if (previous == position) 
       throw new IllegalStateException(); 

      if (position == head) 
      { 
       removeFirst(); 
       currentSize --; 
       tails = previous; 
      } 
      else 
      { 
       previous.next = position.next; 
       currentSize --; 
       tails = previous; 
      } 

      position = previous; 

     } 

     /** 
     Sets the last traversed element to a different 
     data. 
     @param element the element to set 
     */ 
     public void set(Object element) 
     { 
      if (position == null) 
       throw new NoSuchElementException(); 
      position.data = element; 
     } 


    } 
} 

的錯誤是在removeFirst()

任何幫助?

編輯: 我試圖存儲最後引用的節點,所以我可以用它別的地方玩。

+3

太多的代碼,嘗試構建一個最小的測試用例。 – Dukeling 2013-03-01 16:46:53

+0

什麼這些線在removeFirst()目的: 如果(tails.next == NULL) tails.next =頭; – PierrOz 2013-03-01 17:00:01

+0

我做了編輯。代碼適用於我們在計算機科學領域所做的一個項目;我不是在尋求答案,而是在正確的方向上尋找我應該尋找的東西。 – Anonimou5e 2013-03-01 17:12:32

回答

0

通常是錯誤意味着無限遞歸。我建議你看看下面的代碼部分。

while(it.hasNext()){ 
      temp.append(it.next()); 
      if(it.hasNext()) 
       temp.append(", "); 
     } 
+0

那麼我加入此: 而(it.hasNext()){ temp.append(it.next()); 如果(it.hasNext()) temp.append( 「」); } if(!it.hasNext()) temp.next(); 但這似乎並沒有這樣做... – Anonimou5e 2013-03-01 17:05:59

+0

現在,我想起它,這將無法正常工作......嘆息 – Anonimou5e 2013-03-01 17:10:33