2011-11-07 23 views
1

我正在嘗試完成一個程序,並且遇到了一個現場。我的deleteCurrentNode方法只有部分工作。出於某種原因,當我嘗試遍歷鏈表來查找currentNode時,它從來沒有找到它。有人可以向我提供如何使其工作的技巧嗎?如何刪除單鏈表中的當前節點?

的方法本身檢查4個條件:

  1. 如果該列表爲空。
  2. 如果currentNode爲空。
  3. 如果currentNode是列表中的第一個節點。
  4. 如果currentNode在列表中的某處。

其他條件的工作(據我所知)。 4是問題所在。

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); 

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


     for(int i=0; i< dataTest.length(); i++) { test.insert(new String(new char[] { dataTest.charAt(i) })); test.last(); } 
     System.out.println("[5] "+test); 

    while(!test.isEmpty()) { test.deleteFirstNode(); } 
    System.out.println("[6] "+test); 

    for(int i=0; i< dataTest.length(); i++) { test.insert(new String(new char[] { dataTest.charAt(i) })); test.last(); } 
    System.out.println("[7] "+test); 

    while(!test.isEmpty()) { test.deleteFirstNode(false); } 
    System.out.println("[8] "+test); 

    for(int i=0; i< dataTest.length(); i++) { test.insertBeforeCurrentNode(new String(new char[] { dataTest.charAt(i) })); test.first(); } 
    System.out.println("[9] "+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("current " + getCurrent()); 
    //System.out.println("pointer " + getListPtr()); 

} 

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() 
{ 
    //System.out.println("in delete current"); 
    Object nData; 
    Node previous; 

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

    checkCurrent(); //check if currentNode is null, method throws exception if it is. 

    nData = getCurrent().getData(); 

    if (getCurrent() == getListPtr()) 
    { 
     setListPtr(getCurrent().getNext()); 
     setCurrent(getCurrent().getNext()); 
     nodeCount = nodeCount -1; 
    } 

    else 
    { 
     previous = getListPtr(); 
     while (previous.getNext() != getCurrent()) 
     { 
      previous = previous.getNext(); 
      //System.out.println("test"+ previous); 
     } 


     if (getCurrent().getNext() != null) 
     { 
      previous.setNext(null); 
     } 

     previous.setNext(getCurrent().getNext());  } 

    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 = getListPtr(); 
    //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 = getListPtr(); 
      //System.out.println(nodeCount); 
      nodeString = ("List contains " + nodeCount + " nodes"); 
      while (sNode != null) 
      { 
       nodeString = nodeString + " " +sNode.getData(); 
       sNode = sNode.getNext(); 
      } 
      return nodeString; 
     } 
    } 


} 

[4]應該讀一樣的[2]

注(列表中包含0的節點。):不能使用任何更多的變量/方法/等我已經有了。而且我不能使用頭節點。

+0

你實際上沒有做任何事情,我不明白你的問題。 – leo

+0

你不會刪除節點,它應該在「前一個」 – leo

+0

中複製我的程序的錯誤版本抱歉。它已被更新。該方法本身應該返回被刪除的當前節點的數據部分,但現在我只是想遍歷列表,以便我的前一個變量位於currentNode。 – solllodolllo

回答

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


public static void main(String[] args) 
{ 
    String  data; 
    Object  hold; 
    LinkedList list; 

    data = "abcdefghijklmnopqrstuvwxyz"; 
    hold = null; 
    list = new LinkedList(); 

    for(int i=0; i< data.length(); i++) { list.insert(new String(new char[] { data.charAt(i) })); } 
    System.out.println("[1] "+list); 

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

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

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

    for(int i=0; i< data.length(); i++) { list.last(); list.deleteCurrentNode(); } 
    System.out.println("[4] "+list); 


    for(int i=0; i< data.length(); i++) { list.insert(new String(new char[] { data.charAt(i) })); list.last(); } 
    System.out.println("[5] "+list); 

    while(!list.isEmpty()) { list.deleteFirstNode(); } 
    System.out.println("[6] "+list); 

    for(int i=0; i< data.length(); i++) { list.insert(new String(new char[] { data.charAt(i) })); list.last(); } 
    System.out.println("[7] "+list); 

    while(!list.isEmpty()) { list.deleteFirstNode(false); } 
    System.out.println("[8] "+list); 

    for(int i=0; i< data.length(); i++) { list.insertBeforeCurrentNode(new String(new char[] { data.charAt(i) })); list.first(); } 
    System.out.println("[9] "+list); 

    list.first(); 
    list.next(); 
    list.deleteFirstNode(true); 
    list.deleteCurrentNode(); 
    for(int i=0; i< 5; i++) { hold = list.getData(); list.next(); } 
    for(int i=0; i< 10; i++) { list.next(); } 
    list.insertAfterCurrentNode(hold); 
    list.first(); 
    list.next(); 
    hold = list.getData(); 
    list.deleteCurrentNode(); 
    for(int i=0; i<9; i++) {list.deleteCurrentNode(); list.last(); } 
    list.insert(hold); 
    list.first(); 
    list.next(); 
    list.next(); 
    list.next(); 
    list.deleteFirstNode(false); 
    hold = list.getData(); 
    list.deleteCurrentNode(); 
    list.last(); 
    list.insertAfterCurrentNode(hold); 
    list.deleteFirstNode(); 
    list.deleteFirstNode(); 
    hold = list.getData(); 
    list.deleteFirstNode(); 
    list.last(); 
    list.insertBeforeCurrentNode(hold); 
    list.first(); 
    for(int i=0; i<6; i++) { list.next(); } 
    hold = list.getData(); 
    list.deleteCurrentNode(); 
    list.last(); 
    list.insertBeforeCurrentNode(hold); 
    list.first(); 
    list.deleteCurrentNode(); 
    list.deleteCurrentNode(); 
    hold = list.getData(); 
    list.deleteCurrentNode(); 
    for (int i=0; i< 7; i++) { list.next(); } 
    list.insertBeforeCurrentNode(hold); 
    for (int i=0; i< 4; i++) { list.first(); list.deleteCurrentNode(); } 
    System.out.println("\n\n"+list); 


} 

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

public boolean atEnd() 
{ 
    if (getCurrent() != null) 
     { 
     return getCurrent().getNext() == null; 
     } 
     return true; 

} 

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("current " + getCurrent()); 
    //System.out.println("pointer " + getListPtr()); 

} 

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() 
{ 
    //System.out.println("in delete current"); 
    Object nData; 
    Node previous; 

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

    checkCurrent(); //check if currentNode is null, method throws exception if it is. 

    nData = getCurrent().getData(); 

    if (getCurrent() == getListPtr()) 
    { 
     setListPtr(getCurrent().getNext()); 
     setCurrent(getCurrent().getNext()); 
     nodeCount = nodeCount -1; 
    } 

    else 
    { 
     previous = getListPtr(); 
     //System.out.println(getCurrent()); 
     //System.out.println(previous + "ptrb "); 
     while (previous.getNext() != getCurrent()) 
     { 
      previous = previous.getNext(); 
      //System.out.println("test"+ previous); 
     } 

     //System.out.println(previous.getNext() == getCurrent()); 

     if (previous.getNext() == getCurrent()) 
     { 
      //System.out.println("say hi"); 
      previous.setNext(getCurrent().getNext()); 
      deAllocateNode(getCurrent()); 
      setCurrent(previous); 
      nodeCount = nodeCount - 1; 
     } 

     previous.setNext(getCurrent().getNext()); 

    } 

    return nData; 
} 

public Object deleteFirstNode(boolean toDelete) 
{ 
    if (toDelete) 
    { 
     setCurrent(getListPtr().getNext()); 
    } 
    deAllocateNode(getListPtr()); 
    setListPtr(getListPtr().getNext()); 

    nodeCount = nodeCount - 1; 
    return getListPtr(); 
} 

public Object deleteFirstNode() 
{ 
    Object deleteFirst; 
    deleteFirst = deleteFirstNode(true); 
    //System.out.println("called"); 
    return deleteFirst; 
} 

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

public String toString() 
{ 
    String nodeString; 
    Node sNode; 
    sNode = getListPtr(); 
    //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 = getListPtr(); 
      //System.out.println(nodeCount); 
      nodeString = ("List contains " + nodeCount + " nodes"); 
      while (sNode != null) 
      { 
       nodeString = nodeString + " " +sNode.getData(); 
       sNode = sNode.getNext(); 
      } 
      return nodeString; 
     } 
    } 


    } 

非常感謝大家的貢獻。他們幫助我解決了我的問題(儘管有些模糊)。

我回答我自己的問題,以便如果其他人碰巧遇到類似的問題,他們可以使用它作爲參考工具。

再一次,謝謝。

1

如果你有一個Node類,代表名單等中的一個節點:

public class Node{ 
public Object value; 
public Node next; 

public Node(){ 

} 

public Node(Object p){ 
    value= p; 
} 

public String toString(){ 
    return "" + this.value.toString(); 
} 
} 

與您在列表實現(假設MYLIST),你將有(方法只能去除):

public class MyList{ 
    private Node head; 

public Object removeFirst(){ 
    if(head == null){ 
     return null; 
    } 

    Object o= head.value; 
    head= head.next; 
    return o; 
} 

    // remove by index 
public Object remove(int i){ 
    int n= this.size(); 
    if(i<0 || i>=n){ 
     return null; 
    } 

    if(i==0){ 
     return this.removeFirst(); 
    } 

    int k=0; 
    Node t= head; 

    while(k < i-1){ 
     t= t.next; 
     k= k+1; 
    } 

    Object o= t.next.value; 
    t.next= t.next.next; 

    return o; 
} 

    //remove by object 
    public boolean remove(Object o){ 
    int k= this.indexOf(o); 
    if(k<0){ 
     return false; 
    } 
    this.remove(k); 

    return true; 
} 

    //not necessary, but you may study the logic 
    public Object removeLast(){ 
    Object o= null; 
    if(head!=null){ 
     if(head.next==null){ 
      o= head.value; 
      head= null; 
     }else{ 
      Node t= head.next; 
      while(t.next.next != null){ 
       t= t.next; 
      } 
      o= t.next.value; 
      t.next= null; 
     } 
    } 
    return o; 
} 
} 

編輯:當你的數組只包含1個元素時,你有一些問題。

例如,在您的最後一個()方法 - >

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

對於我的實現,我必須有兩個不同的節點,即currentNode和firstNode。對於我遇到的方法,我必須在列表中找到currentNode並將其刪除,但我無法讓我的方法正常工作。 – solllodolllo

+0

我發佈了這段代碼,暗示deleteCurrent()應該等於== getIndexOfCurrnet() - > deleteByIndex(indexOfcurrent) – hovanessyan

+0

我無法通過遍歷我的程序找到currentNode(或它的索引)。 在我的程序中,我將本地節點「previous」設置爲列表的開頭。然後,雖然previous.next與當前節點不相等,但是我在while語句結束時遍歷(previous = previous.next。) ,比較previous和getCurrent()的print語句應該顯示爲true,但它不會,這就是我的問題所在。 – solllodolllo

1

你可能要考慮在你的代碼防範null。最好不要做出假設。

例如,

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

} 

可能會更好地寫成

public boolean atEnd() 
{ 
    if (getCurrent() != null) 
    { 
    return getCurrent().getNext() == null; 
    } 
    return true; 
} 

這不一定做到這一點的最好辦法;你可能想扔NoCurrentNodeException什麼的。這取決於你正在尋找的語義。

無論哪種方式,你都不會被埋在NullPointerExceptions中。