C# I';我无法理解下面的c代码片段
我只有这部分代码。我无法理解“下一步”是ptr的另一种方法还是随机的下一种方法。我只是想知道这是否是专家使用的常见代码片段C# I';我无法理解下面的c代码片段,c#,C#,我只有这部分代码。我无法理解“下一步”是ptr的另一种方法还是随机的下一种方法。我只是想知道这是否是专家使用的常见代码片段 Node mystery(Node ptr) { Node temp; Node previous = null; while (ptr != null) { temp = ptr.next; ptr.next = previous; previous = ptr; ptr = temp
Node mystery(Node ptr) {
Node temp;
Node previous = null;
while (ptr != null) {
temp = ptr.next;
ptr.next = previous;
previous = ptr;
ptr = temp;
}
return previous;
}
通常节点对象有一个值(例如int类型)和另一个保存下一个节点的属性
public class Node{
public int Value {get; set;}
public Node Next {get; set;}
//...
}
例如,这里n1
的下一个节点是n2
节点
您可以使用调试器或带笔的纸一步一步地执行逻辑
这看起来像是一个大问题。每个节点指向下一个节点,最后一个注释指向null
head tail
+---+ +---+ +---+
|"A"| next |"B"| next |"C"| next
| |------>| |------>| |------> null
+---+ +---+ +---+
while(ptr!=null){
将循环,直到当前节点为null
head tail
+---+ +---+ +---+
|"A"| next |"B"| next |"C"| next
| |------>| |------>| |------> null
+---+ +---+ +---+
while范围内的代码是一个典型的交换。让我们一步一步地执行它,因为ptr
是包含“a”
的节点,如上所述
temp = ptr.next;
现在,temp
正在指向下一个节点
ptr
|
v
+---+ +---+ +---+
|"A"| next |"B"| next |"C"| next
| |------>| |------>| |------> null
+---+ +---+ +---+
^
|
temp
现在,
ptr.next
指向初始化为空的previous
。ptr
现在从列表中取消链接
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
| ^
| |
temp
ptr
和previous
都指向同一节点
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
^ | ^
| | |
previous temp
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
^ | ^
| | |
previous temp
现在,
ptr
和temp
都指向同一个节点
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
^ | ^
| | |
previous temp
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
^ | ^
| | |
previous temp
由于ptr!=null
为真,循环将继续:
temp = ptr.next;
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
^ | ^
| | |
previous temp
现在,当前节点的下一项是先前保存的项(A“节点):
由于
ptr!=null
为真,循环将继续并应用相同的逻辑:
temp = ptr.next;
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ ^ | +---+ ^
^ | | |
| | |
previous +-----------------temp
现在,
while(ptr!=null)
为false。循环结束并返回previous
,即新头
此方法反转链表的顺序并返回标题。您可以编写反转单个链表的代码 这只是反转单个链接列表的另一个示例: (有关更多详细信息,请参阅)
单击
next
,然后点击F12
。这看起来像是一个非常标准的链表,其中每个节点
都有一个指向其后一个节点的指针,通常称为next
next
不能是一个方法-它必须是一个属性,因为您为它指定了一个值it@SirRufo我猜是一个领域:)这看起来就像是在颠倒一个链表?如果是这样的话:当然,它可能偶尔会用到;也不是每天都会用到;老实说,你可能不应该经常编写自己的列表/集合/etc类型
ptr.next = previous;
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ | +---+
^ | ^
| |
previous temp
previous = ptr;
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ | +---+
^ | ^
| |
previous temp
ptr = temp;
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ | +---+
^ | ^
| |
previous temp
temp = ptr.next;
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ ^ | +---+ ^
^ | | |
| | |
previous +-----------------temp
ptr.next = previous;
ptr
|
v
+---+ +---+ +---+
|"C"| next |"B"| next |"A"| next
| |------> | |------> | |------> null
+---+ +---+ +---+ ^
^ |
| |
previous temp
previous = ptr;
ptr
|
v
+---+ +---+ +---+
|"C"| next |"B"| next |"A"| next
| |------> | |------> | |------> null
+---+ +---+ +---+ ^
^ |
| |
previous temp
ptr = temp;
ptr
|
+---+ +---+ +---+ |
|"C"| next |"B"| next |"A"| next v
| |------> | |------> | |------> null
+---+ +---+ +---+ ^
^ |
| |
previous temp
// C# program for reversing the linked list
using System;
class GFG {
static void Main(string[] args)
{
LinkedList list = new LinkedList();
list.AddNode(new LinkedList.Node(85));
list.AddNode(new LinkedList.Node(15));
list.AddNode(new LinkedList.Node(4));
list.AddNode(new LinkedList.Node(20));
// List before reversal
Console.WriteLine("Given linked list:");
list.PrintList();
// Reverse the list
list.ReverseList();
// List after reversal
Console.WriteLine("Reversed linked list:");
list.PrintList();
}
}
class LinkedList {
Node head;
public class Node {
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
}
// function to add a new node at
// the end of the list
public void AddNode(Node node)
{
if (head == null)
head = node;
else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}
}
// function to reverse the list
public void ReverseList()
{
Node prev = null, current = head, next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
// function to print the list data
public void PrintList()
{
Node current = head;
while (current != null) {
Console.Write(current.data + " ");
current = current.next;
}
Console.WriteLine();
}
}