2014-12-06 46 views
-1

我正在制作一个单向链表,我需要把删除方法,但得到错误任何帮助吗?单链表删除值

public void remove(int index) { 
    getNode(index - 1).Next = getNode(index + 1); 
} 
+2

如果你告诉我们实际的错误是什么它可能帮助。此外,这段代码很可能不足以确定问题。 – jpw 2014-12-06 01:52:39

回答

0

这里可能会出现很多可能的错误。首先,我会在做出任何更改之前进行更多验证,就好像

  • 您是否在第一个元素上(然后标题将是第二个元素);
  • 你在最后一个元素(然后是getNode(index + 1)返回NULL或抛出异常)?
0

像Apokai正确地指出,你需要在删除时非常小心。

下面是删除无论使用哪一个服务于目的的三种方法:

//Method to delete the first/head element 
    public void deleteAtFirst() { 
     if (head == null) {//If list is empty 
      System.out.println("Linked List EMPTY!!"); 

     } else {//else if it is not empty 
      head = head.next;//assigning head the address of the next node 
     } 
    } 

    //Method to delete the last element 
    public void deleteAtLast() { 
     if (head == null) {//If list is empty 
      System.out.println("Linked List EMPTY!!"); 
     } else if (head.next == null) {//else if it has only 2 elements 
      head = null; 
     } else {//else if it is not empty and has more than 2 elements 
      Node tmpNode = head;//taking a temporary node and initialising it with the first/head node 
      while (tmpNode.next.next != null) {//looping it till the 'second last' element is reached 
       tmpNode = tmpNode.next; 
      } 
      tmpNode.next = null;//assigning the next address of the second last node as null thus making it the last node 
     } 
    } 

    //Method to delete an element at a given position 
    //The first element is situated at 'Position:[0]' 
    public void deleteAtPosition(int pos) { 
     int size = getSize();//Getting the size of the linked list through a pre-defined method 
     if (head == null || pos > size - 1) {//if the position is beyond the scope of current list or the list is empty 
      System.out.println("Position: " + pos + "does not exist"); 
     } else { 
      Node prevNode = null; 
      Node currNode = head; 

      if (pos == size - 1) {//if position is equal to size-1 then essentially the last element is to be deleted 
       deleteAtLast(); 
      } else if (pos == 0) {//if position given is '0' then we need to delete the first element 
       deleteAtFirst(); 
      } else {//else if it is any other valid position 
       for (int i = 0; i < pos; i++) {//looping till the desired position 
        prevNode = currNode;//the node just before the required position will be assigned here 
        currNode = currNode.next;//the current node 
       } 
       prevNode.next = currNode.next;//assigning the next address of previous node to the next of current node thus removing the current node from the list 
      } 
     } 
    } 

接招如果你使用的最后一个,则上述两种方法都存在在你的代码照顾。此外,它采用另一种方法的getSize():

//Method to get the size of the list 
    public int getSize() {//return type is integer as we are returning 'int size' 
     if (head == null) {//if the list is empty 
      return 0; 
     } 
     int size = 1;//initialising size by one 
     Node tmpNode = head;//taking a temporary node and initialising it with the first/head node 
     while (tmpNode.next != null) {//looping till the end of the list 
      tmpNode = tmpNode.next;//incrementing node to the next node 'after every iteration' of the loop 
      size++;//incrementing the size 
     } 
     return size;//returning size 
    }