[英]Understanding linked lists (Java)
can someone please tell me if I am correct? 有人可以告诉我我是否正确吗? I am studying for a midterm.
我正在学期中。
x is a variable pointing to a linked-list node and not the last node on the list. x是指向链表节点的变量,而不是链表中的最后一个节点。 t points to a new node that is not in the list.
t指向不在列表中的新节点。
x.next = t;
t.next = x.next;
I believe when it comes time to update t.next, x.next is no longer the original node following x, but is instead t itself. 我相信当需要更新t.next时,x.next不再是x之后的原始节点,而是t本身。 So it create a cycle in the list
因此它在列表中创建一个循环
t = x.next
x = t;
I believe this does nothing to the list. 我相信这对列表没有任何帮助。
Thank you in advance!! 先感谢您!!
You can also do it threadsafe like this: 您也可以这样进行线程安全:
t.next = x.next; // let t and x point to the SAME next.
x.next = t; // change the x.next to t(who has the old next)
In this case store node in temp
variable. 在这种情况下,将节点存储在
temp
变量中。 It won't create the cycle. 它不会创建周期。
Object temp = x.next;
x.next = t;
t.next = temp;
First you have list like this.. 首先,您有这样的列表。
X--->Y----->Z-->
You want to insert a node t
after X
您想在
X
之后插入节点t
Right now t
is 现在
t
是
t---->null
Step 1- Now we have temp
pointing to X's next 步骤1-现在,我们
temp
指向X的下一个
x---->y----->z----->
^
|
temp--
Step 2- Now x's next is pointing to t
步骤2-现在x的下一个指向
t
x----->t---->
now main list is like this 现在主列表是这样的
temp---->y---->z---->
Step 3- Now t's next is pointing to temp which is only next
pointer 步骤3-现在t的下一个指向温度,temp是仅
next
指针
temp---->y--->z---->
^
|
----------
|
x---->t---
So resulting list is 所以结果列表是
x--->t---->y---->z----->
You already have object x
. 您已经有对象
x
。 This probably the current last element of the linked list. 这可能是链表的当前最后一个元素。 Now, you create a new object
T
and link it as the element after X
现在,您创建一个新对象
T
并将其链接为X
之后的元素
X // Lets assume X.next == NULL. So linked list looks like this X -> Null
X.next = T // Now X.next == T and T.Next == NULL, So linked list looks like this X -> T -> Null.
T.next = X.next // Now T.next == T. So linked list is X -> T <->T
This way, when you reach the end of the linked list, it will always return the last element instead of returning NULL
. 这样,当您到达链接列表的末尾时,它将始终返回最后一个元素,而不是返回
NULL
。
If you are writing a simple algorithm for this, first you have to create an element and then point its next
variable to it self. 如果要为此编写简单的算法,则首先必须创建一个元素,然后将其
next
变量指向其自身。 <First_element>.next = <First_element>
. <First_element>.next = <First_element>
。 So the logic will work for all the instances. 因此,该逻辑将适用于所有实例。
Here is a simple experiment. 这是一个简单的实验。
class Node{
Node next = null;
int id =-1;
}
public class LinkedList{
public static void main (String args[]){
Node x = new Node();
x.id = 0;
x.next = x;
// Now add a new element
Node t = new Node();
t.id =1;
x.next = t;
t.next = x.next; // Now we have a linked list of 2 elements
Node mynode = x;//First element of linked list
for(int i =0; i < 3; i++){
System.out.println(mynode.id);
mynode = mynode.next;
}
}
}
Output: 输出:
0
1
1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.