简体   繁体   English

请有人帮我解释一下链表吗?

[英]please can some one help me explain linked list?

我已经花了很多时间来学习链表,但是我的所有工作都浪费了,请提供一个自己的代码可以帮助我理解链表吗?

A linked list is simply a list of elements (usually called nodes) where each node has a reference (or pointers, in C) to the next node: 链表只是元素(通常称为节点)的列表,其中每个节点都有对下一个节点的引用(或C语言中的指针):

http://img837.imageshack.us/img837/5613/ll1s.png http://img837.imageshack.us/img837/5613/ll1s.png

You keep track of the list by having a pointer to the first node (the "head"), and by having the last node point to null 通过使指针指向第一个节点(“头”)并使最后一个节点指向null来跟踪列表

替代文字

Linked lists where each element points to both the next and previous nodes are called doubly-linked lists. 每个元素都指向下一个节点和上一个节点的链接列表称为双重链接列表。

替代文字

By following these references, you can traverse the list and get any node. 通过遵循这些参考,您可以遍历列表并获取任何节点。

A common advantage of linked lists over arrays is that you can insert and remove the elements in O(1) (constant) time. 链表相对于数组的一个共同优点是,您可以在O(1)(恒定)时间内插入和删除元素。 The disadvantage is that you have O(N) random-access. 缺点是您具有O(N)个随机访问权限。

See Wikipedia for more. 有关更多信息,请参见Wikipedia

Have you play some of these rally games? 您玩过其中一些拉力赛吗? The organizer left this hints all around the city and you must get one hint and then solve the riddle for getting the position of the next hint. 组织者在整个城市中留下了此提示,您必须先获得一个提示,然后解决谜题以获取下一个提示的位置。 Now, imagine every hint comes with a little prize. 现在,想象每一个提示都会带来一点奖励。

Well, linked list are like that: every element has "content" on it AND the memory address (the hint) for getting the next item. 好吧,链接列表是这样的:每个元素上都有“内容”以及用于获取下一个项目的内存地址(提示)。 The next item, of course, has another prize and another hint. 当然,下一项有另一个奖项和另一个提示。

A linked list is a series of object each pointing to the next one in the list. 链接列表是一系列对象,每个对象都指向列表中的下一个对象。 The last element in the list has NULL as it's next pointer. 列表中的最后一个元素的next指针为NULL

You keep track of the head of the list in your program so you can traverse the list from the start. 您可以在程序中跟踪列表的开头,因此可以从头开始遍历列表。

You might want to keep track of the current position in the list, but that will depend on your application. 您可能想要跟踪列表中的当前位置,但这取决于您的应用程序。

A doubly linked list has a previous element pointer too enabling you to traverse the list in both directions. 双链列表还具有一个先前的元素指针,使您可以双向浏览该列表。

This: 这个:

typedef struct tagProp
{
   rtPropertyKey   key;
   rtProperty      property;
   struct tagProp *next;
} TProperty;

defines a property key/value lookup list. 定义属性键/值查找列表。

A linked list is implemented as a list of "nodes". 链接列表被实现为“节点”列表。 The nodes are linked together using pointers. 使用指针将节点链接在一起。 The following code describes one node. 以下代码描述了一个节点。 The pointer to the next node is called next. 指向下一个节点的指针称为next。 In my example, each node contains an integer value as its data. 在我的示例中,每个节点都包含一个整数值作为其数据。


struct node {
   int val;
   struct node * next;
};

The fun is how to actually create a list. 有趣的是如何实际创建列表。 You have to use malloc to create new nodes. 您必须使用malloc创建新节点。 When you malloc the new node, you have to tie into the list using the next pointer. 当您分配新节点时,必须使用下一个指针将其绑定到列表中。

We can help you more if you specifically tell us what your issues are... 如果您明确告诉我们您的问题是什么,我们可以为您提供更多帮助...

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

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