简体   繁体   English

在单个链接列表中添加节点

[英]Add a node in a single linked list

Good evening 晚上好

I'm trying to implement a single linked list by myself and I've run into an issue when I want to create a search method. 我正在尝试自己实现一个链接列表,当我想创建搜索方法时遇到了一个问题。 Evidently when you want to search for a node (which will be used to insert a node at a certain place) you will have to evaluate some values to see if you reached the right spot. 显然,当您要搜索节点(用于在特定位置插入节点)时,必须评估一些值以查看是否到达正确的位置。 Considering my nodes only have a data field as a identifier, I don't see any other way than using that. 考虑到我的节点仅具有一个数据字段作为标识符,除了使用该字段外,我看不到其他任何方法。 However, since the data field isn't unique there might be multiple nodes eligible. 但是,由于数据字段不是唯一的,因此可能有多个符合条件的节点。

Consider the following list: 5, 7, 2, 8, 3, 1, 6, 5, 8, 4, 2. When I want to add a node somewhere in the list (say: After the node with value 8) he will go trough the list and add the new node after the first occurrence of '8'. 考虑以下列表:5、7、2、8、3、1、6、5、8、4、2。当我想在列表中的某个位置添加节点时(例如:在值为8的节点之后),他将遍历列表,并在首次出现“ 8”之后添加新节点。 What should I do if I wanted to insert it after the 2nd 8? 如果我想在2nd 8之后插入该怎么办?

Is this even possible with a Single Linked List? 使用单个链接列表甚至可能吗?

Other than that I'd like to have some feedback on my 'removeLast()' method which doesn't seem to do what I want it to do (remove the last node from the list). 除此之外,我还想对我的'removeLast()'方法获得一些反馈,该方法似乎并没有执行我想要的操作(从列表中删除最后一个节点)。 I am aware my code isn't supposed to work if the list has only 1 value, I'll look into that as soon as the general code of removing the last node works. 我知道如果列表中只有1个值,则我的代码不起作用,一旦删除最后一个节点的常规代码起作用,我将立即进行调查。

My code can be found here . 我的代码可以在这里找到。

Edited with code: 用代码编辑:

 public class SingleLinkedList {

 public void deleteLast() {
    if (lastNode != null) {
        Node currentNode = firstNode;

        while (currentNode != null) {
            Node nextNode = currentNode.getNextNode();
            Node nextNextNode = nextNode.getNextNode();

            if (nextNextNode == null) {
                nextNextNode = null;
                lastNode = nextNode;
            }
        }
        listSize--;
    }
}

} }

Sure it can be done - you need to keep track of the number of objects you have passed in the way, and after you have passed n objects equals to the seeked one - insert the new data: 当然可以做到-您需要跟踪已传递的对象的数量,并且在传递n对象等于找到的对象之后-插入新数据:

public void addAfterNth(Object data,Object o, int n) { 
    Node curr = firstNode;
    while (curr != null) { 
        if (curr.data.equals(o)) n--;
        if (n == 0) { 
            Node newNode = new Node(data,curr.nextNode);
            curr.setNext(newNode);
            break;
        }
        curr = curr.getNextNode();
    }
}

In here you insert a new node with the data denoted in the parameter data after the n th encounter of a node with data equals to o . 在这里,在第n次遇到数据等于o的节点后,插入一个新节点,并在参数data中表示该data

Running with: 运行:

SingleLinkedList list = new SingleLinkedList();
list.addLast(5);
list.addLast(7);
list.addLast(2);
list.addLast(8);
list.addLast(3);
list.addLast(1);
list.addLast(6);
list.addLast(5);
list.addLast(8);
list.addLast(4);
list.addLast(2);
list.drawList();
list.addAfterNth(999,8, 2);
System.out.println("");
list.drawList();

yields (as expected): 产量(预期):

5, 7, 2, 8, 3, 1, 6, 5, 8, 4, 2, 
5, 7, 2, 8, 3, 1, 6, 5, 8, 999, 4, 2, 

Here is the pseudo code for deleting the last code of a LL. 这是用于删除LL的最后一个代码的伪代码。 The above answer correctly answers your question of inserting at a specific position. 上面的答案正确回答了您在特定位置插入的问题。

if (START == NULL){
    Print: Linked-List is empty.
}
else{
    PTR = START, PREV = START
    while (PTR->LINK != NULL)enter code here
        PREV = PTR //Assign PTR to PREV
    PTR = PTR->LINK //Move PTR to next node

    ITEM = PTR->INFO //Assign INFO of last node to ITEM
    If (START->LINK == NULL) Then //If only one node is left
        START = NULL //Assign NULL to START
    Else
        PREV->LINK = NULL //Assign NULL to link field of second last node

    Delete PTR
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM