2015-02-11 60 views
0

我正在爲我的數據結構類進行分配。我們必須使用我們自己的已排序鏈接列表adt創建通訊錄。現在,add方法起作用,但它似乎使所有節點都指向第一個節點。每當我嘗試在for循環中使用getEntry()輸出列表時,它每次都會給我最後一個添加的條目。我試過使用toArray,但它做同樣的事情。你能看到任何問題嗎?Sorted Linked Based List Java

public class GTSortedLinkedBasedList implements GTListADTInterface { 
private Node firstNode; 
private int numberOfEntries; 

public GTSortedLinkedBasedList(){ 
    //firstNode = new Node(null); 
    numberOfEntries = 0; 
} 

public void setNumberOfEntries(int x){ 
    numberOfEntries = x; 
} 

public void add(ExtPersonType newEntry){ 
    //firstNode = null; 
    Node newNode = new Node(newEntry); 
    Node nodeBefore = getNodeBefore(newEntry); 
    if (isEmpty() || (nodeBefore == null)) 
    { 
     // Add at beginning 
     newNode.setNextNode(firstNode); 
     firstNode = newNode; 
    } 
    else 
    { 
     // Add after nodeBefore 
     Node nodeAfter = nodeBefore.getNextNode(); 
     newNode.setNextNode(nodeAfter); 
     nodeBefore.setNextNode(newNode); 
    } // end if 
    numberOfEntries++; 
} 

private Node getNodeBefore(ExtPersonType anEntry){ 
    Node currentNode = getFirstNode(); 
    Node nodeBefore = null; 
    while ((currentNode != null) && 
    (anEntry.getFirstName().compareTo(currentNode.getData().getFirstName()) > 0)) 
    { 
    nodeBefore = currentNode; 
    currentNode = currentNode.getNextNode(); 
    } // end while 
    return nodeBefore; 
} 

private class Node { 

    private ExtPersonType data; 
    private Node next; 

    public Node(ExtPersonType dataValue) { 
     next = null; 
     data = dataValue; 
    } 

    public Node(ExtPersonType dataValue, Node nextValue) { 
     next = nextValue; 
     data = dataValue; 
    } 

    public ExtPersonType getData(){ 
     return data; 
    } 
    public void setData(ExtPersonType newData){ 
     data = newData; 
    } 
    public Node getNextNode(){ 
     return next; 
    } 
    public void setNextNode(Node newNode){ 
     next = newNode; 
    } 

} 


public ExtPersonType getEntry(int givenPosition) { 
    if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)){ 
     assert !isEmpty(); 
     return getNodeAt(givenPosition).getData(); 
    } 
    else{ 
     throw new IndexOutOfBoundsException("Illegal position given to getEntry operation."); 
    } 
} 

public void loadData(GTSortedLinkedBasedList contacts) throws FileNotFoundException{ 
    //int index = 0; 
    ExtPersonType person = new ExtPersonType(); 
    DateType tempDate = new DateType(); 
    AddressType tempAddress = new AddressType(); 
    Scanner file = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt")); 
    while(file.hasNext()){ 
     person.setFirstName(file.next()); 
     person.setLastName(file.next()); 
     tempDate.setMonth(file.nextInt()); 
     tempDate.setDay(file.nextInt()); 
     tempDate.setYear(file.nextInt()); 
     person.setDOB(tempDate); 
     tempAddress.setStreetAddress(file.nextLine()); 
     if(tempAddress.getStreetAddress().isEmpty()){ 
      tempAddress.setStreetAddress(file.nextLine()); 
     } 
     tempAddress.setCity(file.nextLine()); 
     tempAddress.setState(file.nextLine()); 
     tempAddress.setZipCode(file.nextLine()); 
     person.setAddress(tempAddress); 
     person.setPhoneNumber(file.nextLine()); 
     person.setPersonStatus(file.nextLine()); 
     if(person.getPersonStatus().isEmpty()){ 
      person.setPersonStatus(file.nextLine()); 
     } 
     contacts.add(person); 
     System.out.println(contacts.getEntry(contacts.getLength()).getFirstName()); 
     //index++; 

    } 
} 

public static void main(String[] args) throws FileNotFoundException { 
    AddressBook ab = new AddressBook(); 

    ab.loadData(ab); 
    ExtPersonType people = new ExtPersonType(); 
    //people = ab.toArray(people); 
    System.out.println(ab.getLength()); 
    for(int cnt = 1; cnt <= ab.getLength(); cnt++){ 
     people = ab.getEntry(cnt); 
     System.out.println(people.getFirstName()); 
    } 

} 

編輯:添加方法是用新添加的每個以前的對象覆蓋。這似乎也不重要,如果我做一個排序列表或只是一個基本列表。

+0

「眼下add方法的工作原理,但它似乎使所有節點指向第一個節點。」 - >所以你說的是它根本不起作用? – JNYRanger 2015-02-11 01:04:39

+0

哈哈,這是真的。我發現每次添加對象時,都會用新對象覆蓋列表中的所有對象。 – 2015-02-11 01:08:24

+0

您的問題出現在getNodeBefore(ExtPersonType)代碼中,因爲您可以成功添加第一個節點。您是否已經實施了非排序鏈接列表?如果是這樣,那麼執行...然後調整它。如果這是通用的,您可能希望讓構造函數使用[Comparator](http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html)對象,以便您可以比較值查看每個節點的屬性。如果不是通用的,請實現您自己的compareTo,它需要一個ExtPersonType。請記住,您可能需要有時替換您的頭節點... – JNYRanger 2015-02-11 01:13:19

回答

0

我不會在這裏說謊,我不完全確定我理解你的代碼,但我想我明白了什麼是錯的。在getNodeBefore()方法的代碼中,您將currentNode()始終設置爲firstNode()。我相信這是造成這個問題的原因。我發現你試圖通過遞歸遍歷列表來找到合適的節點,但我認爲每次遞歸調用都不會導致整個列表的移動。我建議你向代表向前和向後節點的對象添加屬性。

事情是這樣的......

private T data; 
private Node nodeBefore; 
private Node nodeAfter; 

當你創建對象時,前,後分配屬性,然後所有你需要的信息都包含在對象本身。

要在列表中遞歸移動,您只需添加一條語句,如currentNode = currentNode.nodeAfter。

您的getNodeBefore()方法將簡單地返回currentNode.nodeBefore,getNodeAfter()將返回currentNode.nodeAfter。

+0

哪裏需要添加語句currentNode = currentNode.nodeAfter; ? – 2015-02-11 01:38:41

+0

當你想循環列出那個方向。這將是一種新方法。根據你想要/需要做什麼,這可能是不必要的。 – richtarj 2015-02-11 17:17:35

0

您沒有處理所添加的節點將成爲列表中的第一個節點但列表也不爲空的情況的代碼。在這種情況下,getNodeBefore返回null,並且您的代碼將覆蓋根節點。

嘗試

if (isEmpty() && (nodeBefore == null)) 
    { 
    // Add at beginning 
    newNode.setNextNode(firstNode); 
    firstNode = newNode; 
    } 
    else if(nodeBefore == null) 
    { 
    Node temp = new Node(); 
    temp.setNextNode(first.next); 
    temp.setData(first.data); 
    newNode.setNextNode(temp); 
    firstNode = newNode; 
    }