简体   繁体   English

C#内存分配和链接列表实现

[英]C# Memory Allocation & Linked List Implementation

I have some questions about how exactly C# implement the linked list class first question: 我对C#如何实现链表类的第一个问题有一些疑问:

public class Node {  
    int data;  
    Node next;   
} 

it looks like the rather than a linked list, it is a Big object with recursive same class inside 它看起来像是一个而不是一个链表,它是一个Big对象,里面有递归的同类

my second question is more specific, for a simple reverse linked list algorithm like below: 我的第二个问题更具体,对于一个简单的反向链表算法,如下所示:

 public void ReverseList(ref ListNode head){
    if(head ==null || head.next == null) return head;
    ListNode cur = head;
    ListNode prev = null;
    ListNode next = head.next;
    while (cur != null){
        ListNode nextNode = head.next;
        cur.next= prev;
        prev=cur;
        cur=nextNode;
    }
    head = prev ;
} 

isn't cur = head means the "cur" copy ALL linked list elements? 是不是cur = head意味着“cur”复制所有链表元素? And it is much harder to think what was going on in the algorithm. 并且很难想象算法中发生了什么。

it looks like the rather than a linked list, it is a Big object with recursive same class inside 它看起来像是一个而不是一个链表,它是一个Big对象,里面有递归的同类

Node is a class , which means that the field Node next; Node是一个class ,表示Node next;字段Node next; is a reference (broadly interchangeable with a pointer, in terms of terminology). 是一个参考(在术语方面可广泛地与指针互换)。 So no: it is definitely a linked list. 所以没有:它绝对是一个链表。 Node doesn't contain a Node . Node包含 Node It contains a field which is a reference (think: pointer) to another Node . 它包含一个字段,它是另一个Node的引用(思考:指针)。

isn't cur = head means the "cur" copy ALL linked list elements? 是不是cur = head意味着“cur”复制所有链表元素?

It isn't copying any elements. 它没有复制任何元素。 It is changing the reference field (think: pointer) on some existing objects, and that's it . 它正在改变一些现有对象上的引用字段(思考:指针) ,就是这样 No copying. 没有复制。 It is, however, visiting all the elements and changing the pointers on them all - so if you count "copying the references (think: pointers) on all elements": sure, it is doing that. 然而,它访问所有元素并更改它们上的所有指针 - 所以如果你计算“复制所有元素上的引用(思考:指针)”:当然,它正在这样做。 There aren't any allocations involved, etc. 没有任何分配,等等。

Linked list uses references to create the list structure. 链接列表使用引用来创建列表结构。

The key point is to understand the distinction between value and reference types in C#. 关键是要理解C#中引用类型之间的区别。 Value types like int , bool or struct are really values that are stored in stack and when you assign them, you are actually copying the data from one location to another. intboolstruct这样的值类型实际上是存储在堆栈中的值,当您分配它们时,实际上是将数据从一个位置复制到另一个位置。 Reference types are classes in C# and the distinction is that they are just pointers to data in memory heap. 引用类型是C#中的classes ,区别在于它们只是指向内存堆中数据的指针。 By default, they are null and you first must allocate memory for them on the heap so that they actually point somewhere using new . 默认情况下,它们为null ,您首先必须在堆上为它们分配内存,以便它们实际指向使用new

In this case you have a field of type Node as a property of the Node class, which is fine, because this is either just null or it points to another location in memory heap where the next node is stored. 在这种情况下,您有一个Node类型的字段作为Node类的属性,这很好,因为它只是null或它指向存储下一个节点的内存堆中的另一个位置。 If you changed the class to struct , this would not compile, as this would cause the "recursive" problem you are mentioning. 如果您将class更改为struct ,则无法编译,因为这会导致您提到的“递归”问题。 But with classes it is just a reference and the default value is null , so it doesn't have to point to an instance and instantiating a Node creates just one Node instance in memory, with next == null . 但是对于类,它只是一个引用,默认值为null ,因此它不必指向实例,并且实例化Node只在内存中创建一个Node实例, next == null

This should help you understand the second question as well - setting next = head means you are just making next point to the same place head is pointing in memory. 这应该可以帮助你理解第二个问题 - 设置next = head意味着你只是将next点指向同一个地方, head指向内存。 You are not moving any data, you are just setting the reference. 您没有移动任何数据,您只是设置参考。

链接列表

I find it helpful to use images when building linked list algorithms. 我发现在构建链表算法时使用图像很有帮助。 you can imagine each next field just as an arrow that points somewhere. 你可以想象每个next领域就像指向某个地方的箭头一样。 And when the value is null it just points "nowhere". 当值为null它只是指向“无处”。 Finally, when you assign something to next you just change where the arrow points. 最后,当您为next分配内容时,只需更改箭头指向的位置即可。

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

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