请有人帮我解释一下链表吗?
我已经尝试了很多来学习链表。但是我所有的努力都被浪费了。请有人通过提供他/她自己的代码来帮助我理解链表吗?先谢谢了。
I have tried a lot to learn linked list.But all my efforts were wasted.Please can some one help me understand linked list by providing his/her own code?Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
链表只是一个元素列表(通常称为节点),其中每个节点都有一个指向下一个节点的引用(或指针,在 C 中):
http://img837.imageshack.us/img837/5613/ll1s.png
您可以通过指向第一个节点(“头”)的指针来跟踪列表,并通过最后一个节点指向
null
每个元素都指向下一个元素的链接列表和之前的节点称为双向链表。
通过遵循这些引用,您可以遍历列表并获取任何节点。
与数组相比,链表的一个常见优点是您可以在 O(1)(常数)时间内插入和删除元素。缺点是你有 O(N) 随机访问。
有关更多信息,请参阅维基百科。
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:
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
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. The disadvantage is that you have O(N) random-access.
See Wikipedia for more.
你玩过这些拉力赛游戏吗?组织者在城市各处留下了这个提示,你必须得到一个提示,然后解开谜语以获得下一个提示的位置。现在,想象一下每一个提示都会有一个小奖品。
嗯,链表就像这样:每个元素都有“内容”以及用于获取下一个项目的内存地址(提示)。当然,下一个项目有另一个奖品和另一个提示。
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.
链表是一系列对象,每个对象都指向列表中的下一个对象。列表中的最后一个元素具有
NULL
,因为它是next
指针。您可以在程序中跟踪列表的头部,以便可以从头开始遍历列表。
您可能想要跟踪列表中的当前位置,但这取决于您的应用程序。
双向链表也有一个前一个元素指针,使您能够双向遍历列表。
这:
定义属性键/值查找列表。
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'snext
pointer.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:
defines a property key/value lookup list.
链表被实现为“节点”列表。节点使用指针链接在一起。下面的代码描述了一个节点。指向下一个节点的指针称为next。在我的示例中,每个节点都包含一个整数值作为其数据。
有趣的是如何实际创建一个列表。您必须使用 malloc 来创建新节点。当您 malloc 新节点时,您必须使用下一个指针绑定到列表中。
如果您具体告诉我们您的问题是什么,我们可以为您提供更多帮助...
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. In my example, each node contains an integer value as its data.
The fun is how to actually create a list. You have to use malloc to create new nodes. 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...