【问题标题】:Singly Linked List Delete Value单链表删除值
【发布时间】:2014-12-06 01:41:53
【问题描述】:

我正在制作一个单链表,我需要在其中放置一个删除方法但遇到错误有什么帮助吗?

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

【问题讨论】:

  • 如果您告诉我们实际错误是什么可能会有所帮助。此外,这个代码片段很可能不足以确定问题。

标签: java data-structures linked-list nodes


【解决方案1】:

这里可能会发生许多可能的错误。一方面,我肯定会在进行任何更改之前进行更多验证,就好像

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

【讨论】:

    【解决方案2】:

    就像 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
        }
    

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 2020-08-11
      • 2021-06-15
      • 2016-07-30
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      相关资源
      最近更新 更多