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

发布于 2024-09-15 22:05:01 字数 65 浏览 5 评论 0原文

我已经尝试了很多来学习链表。但是我所有的努力都被浪费了。请有人通过提供他/她自己的代码来帮助我理解链表吗?先谢谢了。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聆听风音 2024-09-22 22:05:01

链表只是一个元素列表(通常称为节点),其中每个节点都有一个指向下一个节点的引用(或指针,在 C 中):

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

您可以通过指向第一个节点(“头”)的指针来跟踪列表,并通过最后一个节点指向 null

alt text

每个元素都指向下一个元素的链接列表和之前的节点称为双向链表。

alt text

通过遵循这些引用,您可以遍历列表并获取任何节点。

与数组相比,链表的一个常见优点是您可以在 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

alt text

Linked lists where each element points to both the next and previous nodes are called doubly-linked lists.

alt text

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.

╰つ倒转 2024-09-22 22:05:01

你玩过这些拉力赛游戏吗?组织者在城市各处留下了这个提示,你必须得到一个提示,然后解开谜语以获得下一个提示的位置。现在,想象一下每一个提示都会有一个小奖品。

嗯,链表就像这样:每个元素都有“内容”以及用于获取下一个项目的内存地址(提示)。当然,下一个项目有另一个奖品和另一个提示。

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.

等你爱我 2024-09-22 22:05:01

链表是一系列对象,每个对象都指向列表中的下一个对象。列表中的最后一个元素具有 NULL,因为它是 next 指针。

您可以在程序中跟踪列表的头部,以便可以从头开始遍历列表。

您可能想要跟踪列表中的当前位置,但这取决于您的应用程序。

双向链表也有一个前一个元素指针,使您能够双向遍历列表。

这:

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

定义属性键/值查找列表。

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.

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.

执手闯天涯 2024-09-22 22:05:01

链表被实现为“节点”列表。节点使用指针链接在一起。下面的代码描述了一个节点。指向下一个节点的指针称为next。在我的示例中,每个节点都包含一个整数值作为其数据。


struct node {
   int val;
   struct node * 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.


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. 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文