Linked list 链表,在节点后添加值
我现在正在学习链表,但很快就被卡住了。 我在读讲师的代码,有一种方法我不太懂Linked list 链表,在节点后添加值,linked-list,Linked List,我现在正在学习链表,但很快就被卡住了。 我在读讲师的代码,有一种方法我不太懂 public class List { public Item start = null; private class Item { private int value; private Item next; private Item (int x, Item item) { value = x; nex
public class List {
public Item start = null;
private class Item {
private int value;
private Item next;
private Item (int x, Item item) {
value = x;
next = item;
}
}
public void insertAfter(Item item, int x) {
if (item == null) {
start = new Item(x, start);
} else {
item.next = new Item(x, item.next);
}
}
public Item findValue(int x) {
Item p = start;
while (p != null && p.value != x) {
p = p.next;
}
return p;
}
假设我有一张清单
9->7->5->4->7->null
使用方法项
L.findValue(7);
返回节点7->5->4->7->null
然后我用
L.insertAfter(item, 100);
为什么这份名单现在会变成
9->7->100->5->4->7->null
我在这里看到的问题是void方法不修改开始节点,而是只修改作为输入的项。
我想我只会修改项目而已
我在这里遗漏了什么???显示insertAfter调用引用标记为“item”的变量的代码段。。。。它将取决于该项在LL中的位置,不一定是'value'属性,因为findValue函数返回值的'first'实例是的,我知道findValue返回第一个实例。因此,如果我使用L.insertAfteritem,100;这意味着我将在前7个之后添加100,但是为什么item.next=newitemx,item.next;是否修改列表的开始节点?