首页 > 解决方案 > 给定链表中的特定节点时如何使用 insertAfter()

问题描述

// insert a new data after the given one
public void insertAfter(int givenData, int newData){
   // Your code here
   Node previous = new Node(givenData);
    if (previous == null) {
        //System.out.println("The given previous node cannot be null.");
        return;
    }

    Node newNode = new Node(newData);
    newNode.next = previous.next;
    previous.next = newNode;
}

// Removes the Node with the given data
public void remove(int current) {
  // Your code here
  Node previous = new Node(current);
    int count = 1;
    int position = 0;
    while (count < position -1) {
        previous = previous.next;
        count++;
    }
    Node curNode = previous.next;
    previous.next = curNode.next;
    curNode.next = null;
}

此代码没有给出任何错误消息,但不会添加或删除节点。我相信我的困惑在于int givenData以及如果它不是节点如何访问它。第一次在这里发帖,希望我提供了足够的信息:)

public class LinkedListTemplate {
Node head;


// inserts data to the end of the list only using the head pointer
public void append(int data){
    Node newNode = new Node(data);
    if(head == null){           
        head = newNode;

    } else {            
        Node currentNode = head;
        while(currentNode.next != null){
            currentNode = currentNode.next;
        }
        currentNode.next = newNode;             
    }
}

// inserts data to the beginning of the list
public void prepend(int data){
    if(head == null){
        Node newNode = new Node(data);
        head = newNode;
        return;
    }
    Node newNode = new Node(data);
    newNode.next = head;
    head = newNode;
}

// print the linked list elements
public void print() {
    Node currentNode = head;
    System.out.printf("[");
    while (currentNode.next != null) {
        System.out.printf("%d, ", currentNode.data);
        currentNode = currentNode.next;
    }
    System.out.printf("%d]%n", currentNode.data);
}

// counts the length of the list
public int length(){
   int length = 0;
   Node currentNode = head;
    while(currentNode != null){
        length++;
        currentNode = currentNode.next;
    }   
    return length;
}

// get an item from the list specified by the index
public int get(int index){
   int len = length();
   if(index > len){
      return -1;
   }

 Node currentNode = head;
 int currentIndex = 0;
 while(currentNode != null){
    if(index == currentIndex){
       return currentNode.data;
    }
    else{
       currentNode = currentNode.next;
       currentIndex++;
     }
  }
  return -1;
}

我包含了其余的链表代码,还有另一个类可以测试列表并允许它运行。

标签: javalinked-listnodes

解决方案


我试了一下。

请注意,我实际上并不知道 remove 真正应该做什么。

您的代码正在寻找具有给定索引的节点,因此我坚持使用该节点(第一个删除版本)。

但评论建议,您需要删除具有给定数据的节点。所以我添加了第二个版本,就是这样做的(第二个删除版本)。

    public void insertAfter(int givenData, int newData) {
        // Your code here
        Node previous = head;
        while (null != previous && previous.data != givenData) {
            previous = previous.next;
        }
        // this prevents insertion if given Data was not found 
        if (previous == null) {
            System.out.println("the given data doesn't exist");
            return;
        }
        Node newNode = new Node(newData);
        newNode.next = previous.next;
        previous.next = newNode;
    }

删除具有给定索引的节点的版本:

    // Removes the Node with the given index
    public void remove(int current) {

        Node previous = head;
        for (int i = 0; i < current - 1; i++) {
            if (null == previous) {
                System.out.println("the given position doesn't exist");
                return;
            }
            previous = previous.next;
        }
        if (current == 0) {
            head = head.next;
        } else if (previous.next == null) {
            System.out.println("the given position is after the end - nothing to do!");
        } else {
            previous.next = previous.next.next;
        }
    }

删除具有给定数据的元素的版本

   // Removes the Node with the given data
    public void remove(int data) {
        Node previous = head;
        if(head == null){
            System.out.println("Empty list - nothing to remove!");
            return;
        } if (head.data == data){
            head = head.next;
            return;
        }

        while(previous.next != null && previous.next.data != data){
            previous = previous.next;
        }
        if (previous.next == null) {
            System.out.println("the given element was not found - nothing to do!");
        } else {
            previous.next = previous.next.next;
        }
    }

推荐阅读