添加一个节点单链表节点、链表

2023-09-11 02:44:07 作者:不萌只是重口味*

大家好

我想实现单链表我本人和我碰到的一个问题,当我想创建一个搜索方法。显然,当你要搜索一个节点(这将被用来在某个地方插入一个节点),你必须评估一些值,看看是否达到正确的位置。考虑到我的节点只能有一个数据字段作为标识符,我没有看到比使用任何其他方式。然而,由于数据字段不是唯一可能有多个节点可享

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.

考虑以下列表:5,7,2,8,3,1,6,5,8,4,2。 当我想在列表中的某个地方添加一个节点(比如:值为8点)之后,他将走线槽名单和'8'第一次出现后添加新节点。我应该怎么做,如果我想第二个8后插入呢?

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'. What should I do if I wanted to insert it after the 2nd 8?

这甚至可能与单链表?

除此之外,我想对我的'removeLast()方法,它似乎并没有做我想做的事情(从列表中删除最后一个节点)的一些反馈。我知道我的code是不应该工作,如果列表中只有1个值,我会考虑,一旦去掉最后一个节点的总体code工作。

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

我的code可以发现这里。

My code can be found here.

编辑与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--;
    }
}

}

推荐答案

当然是可以做到的 - 你需要保持你已经通过的方式对象数量的跟踪,你通过后 N 对象等于给seeked一 - 插入新的数据:

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:

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

在这里您将与表示在参数数据 N 次的邂逅数据的新节点与数据的节点等于 0

In here you insert a new node with the data denoted in the parameter data after the nth encounter of a node with data equals to o.

与运行:

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,