2011-11-07 66 views
0

我想對將插入一個鏈接列表在當前節點之前傳遞給它的節點的方法工作之前插入一個節點。它有3個條件。對於這個實現,不能有任何頭節點(只有對列表中第一個節點的引用),我不能再添加任何變量。鏈表 - 當前節點

  1. 如果列表爲空,則將傳遞的節點設置爲列表中的第一個節點。
  2. 如果當前節點位於列表的前面。如果是這樣,請將傳入的節點設置爲當前節點的旁邊,並將第一個節點設置爲傳遞的節點以將其移至前端。
  3. 如果列表不爲空並且當前不在前面,則遍歷列表,直到本地節點等於列表的當前節點。然後,我執行相同的指令在2

這裏是我的代碼。

public class LinkedList 
{ 
private Node currentNode; 
private Node firstNode; 
private int nodeCount; 

public static void main(String[] args) 
{ 
LinkedList test; 
String dataTest; 
test = new LinkedList(); 
dataTest = "abcdefghijklmnopqrstuvwxyz"; 
for(int i=0; i< dataTest.length(); i++) { test.insert(new String(new char[] { dataTest.charAt(i) })); } 
System.out.println("[1] "+ test); 

    for(int i=0; i< dataTest.length(); i++) { test.deleteCurrentNode(); } 
    System.out.println("[2] "+test); 

    for(int i=0; i< dataTest.length(); i++) 
    { 
    test.insertBeforeCurrentNode(new String(new char[] { dataTest.charAt(i) })); 
    if(i%2 == 0) { test.first(); } else { test.last(); } 
    } 

    System.out.println("[3] "+test); 
} 

public LinkedList() 
{ 
    setListPtr(null); 
    setCurrent(null); 
    nodeCount = 0; 
} 

public boolean atEnd() 
{ 
    checkCurrent(); 
    return getCurrent().getNext() == null; 
} 

public boolean isEmpty() 
{ 
    return getListPtr() == null; 
} 

public void first() 
{ 
    setCurrent(getListPtr()); 
} 

public void next() 
{ 
    checkCurrent(); 
    if (atEnd()) {throw new InvalidPositionInListException("You are at the end of the list. There is no next node. next().");} 
    setCurrent(this.currentNode.getNext()); 
} 

public void last() 
{ 
    if (isEmpty()) {throw new ListEmptyException("The list is currently empty! last()");} 

    while (!atEnd()) 
    { 
     setCurrent(getCurrent().getNext()); 
    } 

} 

public Object getData() 
{ 
    return getCurrent().getData(); 
} 

public void insertBeforeCurrentNode(Object bcNode) //beforeCurrentNode 
{ 
    Node current; 
    Node hold; 
    boolean done; 
    hold = allocateNode(); 
    hold.setData(bcNode); 
    current = getListPtr(); 
    done = false; 
    if (isEmpty()) 
    { 
     setListPtr(hold); 
     setCurrent(hold);  
    } 

    else if (getCurrent() == getListPtr()) 
    { 
     System.out.println("hi" + hold); 
     hold.setNext(getCurrent()); 
     setListPtr(hold); 
    } 

    else //if (!isEmpty() && getCurrent() != getListPtr()) 
    { 
     while (!done && current.getNext() != null) 
     { 
      System.out.println("in else if " + hold); 
      if (current.getNext() == getCurrent()) 
      { 
       //previous.setNext(hold); 
       //System.out.println("hi"+ "yo" + " " + getListPtr()); 
       hold.setNext(current.getNext()); 
       current.setNext(hold); 
       done = true; 
      } 

      //previous = current; 
      current = current.getNext(); 
     } 

    } 
    System.out.println(getCurrent()); 

} 

public void insertAfterCurrentNode(Object acNode) //afterCurrentNode 
{ 
    Node hold; 
    hold = allocateNode(); 
    hold.setData(acNode); 
    if (isEmpty()) 
    { 
     setListPtr(hold); 
     setCurrent(hold); 
     //System.out.println(hold + " hi"); 
    } 

    else 
    { 
     //System.out.println(hold + " hia"); 
     hold.setNext(getCurrent().getNext()); 
     getCurrent().setNext(hold); 
    } 
} 

public void insert(Object iNode) 
{ 
    insertAfterCurrentNode(iNode); 
} 

public Object deleteCurrentNode() 
{ 
    Object nData; 
    Node previous; 
    Node current; 
    previous = getListPtr(); 
    current = getListPtr(); 
    nData = getCurrent().getData(); 

    if (isEmpty()) {throw new ListEmptyException("The list is currently empty! last()");} 

    else if (previous == getCurrent()) 
    { 
     getListPtr().setNext(getCurrent().getNext()); 
     setCurrent(getCurrent().getNext()); 
     nodeCount = nodeCount - 1; 
    } 

    else 
    { 
     while (previous.getNext() != getCurrent()) 
     { 
      previous = current; 
      current = current.getNext(); 


     } 
    previous.setNext(getCurrent().getNext()); 
    setCurrent(getCurrent().getNext()); 
    nodeCount = nodeCount - 1; 
    } 
    return nData; 
} 

public Object deleteFirstNode(boolean toDelete) 
{ 
    if (toDelete) 
    { 
     setListPtr(null); 
    } 
    return getListPtr(); 
} 

public Object deleteFirstNode() 
{ 
    Object deleteFirst; 
    deleteFirst = deleteFirstNode(true); 
    return deleteFirst; 
} 

public int size() 
{ 
    return this.nodeCount; 
} 

public String toString() 
{ 
    String nodeString; 
    Node sNode; 
    sNode = getCurrent(); 
    //System.out.println(nodeCount); 
    nodeString = ("List contains " + nodeCount + " nodes"); 
    while (sNode != null) 
    { 
     nodeString = nodeString + " " +sNode.getData(); 
     sNode = sNode.getNext(); 
    } 
    return nodeString; 
} 

private Node allocateNode() 
{ 
    Node newNode; 
    newNode = new Node(); 
    nodeCount = nodeCount + 1; 
    return newNode; 
} 

private void deAllocateNode(Node dNode) 
{ 
    dNode.setData(null); 
} 

private Node getListPtr() 
{ 
    return this.firstNode; 
} 

private void setListPtr(Node pNode) 
{ 
    this.firstNode = pNode; 
} 

private Node getCurrent() 
{ 
    return this.currentNode; 
} 

private void setCurrent(Node cNode) 
{ 
    this.currentNode = cNode; 
} 

private void checkCurrent() 
{ 
    if (getCurrent() == null) {throw new InvalidPositionInListException("Current node is null and is set to an invalid position within the list! checkCurrent()");} 
} 

/**NODE CLASS ----------------------------------------------*/ 

    private class Node 
    { 
     private Node next; //serves as a reference to the next node 
     private Object data; 

     public Node() 
     { 
      this.next = null; 
      this.data = null; 
     } 


     public Object getData() 
     { 
      return this.data; 
     } 

     public void setData(Object obj) 
     { 
      this.data = obj; 
     } 

     public Node getNext() 
     { 
      return this.next; 
     } 

     public void setNext(Node nextNode) 
     { 
      this.next = nextNode; 
     } 

     public String toString() 
     { 
      String nodeString; 
      Node sNode; 
      sNode = getCurrent(); 
      //System.out.println(nodeCount); 
      nodeString = ("List contains " + nodeCount + " nodes"); 
      while (sNode != null) 
      { 
       nodeString = nodeString + " " +sNode.getData(); 
       sNode = sNode.getNext(); 
      } 
      return nodeString; 
     } 
    } 

} 

我讓它適用於我的[1]和[2]條件。但我的[3](測試insertBeforeCurrentNode())工作不正常。我已經設置了打印語句,並且我確定我的電流在某個地方被重置,但我無法確定哪裏可以使用某些指導或解決方案。

[1]和[2]的輸出是正確的。對於輸出[3]應當讀

[3]列出包含26個節點:Z X V T,; R PÑ升殲轟˚Fd B C例如I K M O對q s操作ü瓦特Y A

感謝提前任何幫助。

+1

當你說「我目前正在某處復位」,你的意思是叫局部變量'current'或該字段稱爲'currentNode'。除此之外:讓變量更容易區分通常是一個好主意。特別是,你的「當前」變量更多的是「候選前」而不是「當前」。 –

+0

我的意思是我的電流被重置。在該方法的最後,我的currentNode除Z之外的每個條目都有,我認爲一旦找到問題就會解決這個問題。 – solllodolllo

回答

2

在您的toString方法中,您開始從currentNode開始打印節點,直到列表的末尾。因爲你只打印您的結果之前調用test.last(),將currentNode將指向列表的最後一個節點上,和你toString()將只打印「A」。

在你toString()方法,你可能要更改

sNode = getCurrent(); 

sNode = getListPtr(); 

打印您的26個節點。

+1

這就像一個魅力。猜猜我的方法工作正常,我需要的只是另一組簡單的東西。謝謝!我很感激。 – solllodolllo

+0

@user - 這是非常普遍的,只是需要另一雙眼睛的簡單的東西。 –

0

爲[3],你需要保持指向兩個節點:一個指針在「當前」節點,你要找的人,另在「上一個」節點,一個僅僅是當前之前。通過這種方式,當您在「當前」位置找到節點時,可以在「前一個」之後和「當前」之前連接新節點。在僞代碼,並確保案件[1] [2]之前已經覆蓋後:

Node previous = null; 
Node current = first; 
while (current != null && current.getValue() != searchedValue) { 
    previous = current; 
    current = current.getNext(); 
} 
previous.setNext(newNode); 
newNode.setNext(current);