链接列表需要帮助无法打印我的数据。想做一个添加功能。在 C - C++
这是我的代码。我想打印我所有的列表数据。但我不能因为当我写 while(llist->next != NULL)
llist->next
是 NULL
但我不'不知道为什么。请帮助我:)
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct rame
{
int data;
struct rame *next;
};
int main()
{
struct rame *llist;
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
llist->next = llist;
llist->next->data = 15;
llist->next->next->data = 20;
llist->next->next->next->data = 25;
llist->next->next->next->next = NULL;
printf("test\n");
if(llist->next == NULL)
printf("%d\n",llist->data);
else
while(llist->next != NULL)
{
printf("%d\n",llist->data);
llist = llist->next;
}
system("pause");
return 0;
}
嘿,我做了一切,但我的循环不打印最后的数据。帮我 :(
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct rame
{
int data;
struct rame *next;
};
int main()
{
struct rame *llist;
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
llist->next = (rame*)malloc(sizeof(struct rame));
llist->next->data = 15;
llist->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->data = 20;
llist->next->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->next->data = 25;
llist->next->next->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->next->next = NULL;
printf("test\n");
while(llist->next != NULL)
{
printf("%d\n",llist->data);
llist = llist->next;
}
system("pause");
return 0;
}
this is my code. i wanted to print all my list datas. but i cant cause when i write while(llist->next != NULL)
llist->next
is NULL
but i don't know why. please help me :)
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct rame
{
int data;
struct rame *next;
};
int main()
{
struct rame *llist;
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
llist->next = llist;
llist->next->data = 15;
llist->next->next->data = 20;
llist->next->next->next->data = 25;
llist->next->next->next->next = NULL;
printf("test\n");
if(llist->next == NULL)
printf("%d\n",llist->data);
else
while(llist->next != NULL)
{
printf("%d\n",llist->data);
llist = llist->next;
}
system("pause");
return 0;
}
hey i did everithing but my LOOP doesnt print the last data. help me :(
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct rame
{
int data;
struct rame *next;
};
int main()
{
struct rame *llist;
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
llist->next = (rame*)malloc(sizeof(struct rame));
llist->next->data = 15;
llist->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->data = 20;
llist->next->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->next->data = 25;
llist->next->next->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->next->next = NULL;
printf("test\n");
while(llist->next != NULL)
{
printf("%d\n",llist->data);
llist = llist->next;
}
system("pause");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
llist
的下一个元素是llist
本身。您本身没有链表,只有一个循环回到自身的元素。所以:所有这些都会修改
llist->data
。并且:将
llist->next
设置为NULL
。如果您想构建列表,则需要创建新的列表元素(使用
malloc
)并链接它们。例如:您的循环不正确:您将始终跳过最后一个条目,因为它的
->next
将为空,因此循环体将不会运行。尝试使用:
您使用第二个指向列表的指针,以便
llist
保持不变并指向列表标题。 (如果您不这样做,您将永远无法返回它,因为它是单链接的。)llist
's next element isllist
itself. You don't have a linked list per se, just a single element that loops back to itself. So:All these modify
llist->data
. And:Sets
llist->next
toNULL
.You'll need to create new list elements (with
malloc
) and link them if you want to build a list. For example:Your loop is not correct: you will always skip the last entry as it's
->next
will be null, and so the loop body will not run.Try with:
You use a second pointer to the list so that
llist
is kept unchanged and pointing at the list header. (If you don't do that, you'll never be able to get back to it since it's singly linked.)在您的代码中
为
llist
分配一个内存位置,并且该位置的数据被分配10
接下来你要做的:
第一行将
llist
的next
链接分配给它自己,这使得列表的状态如下现在执行
llist->next< /code> 指向
llist
本身,因此llist->next->data
只是寻址到list->data
,因此值 10 已更改。在您完成的其他链接中,使用多少次
->next->next->....->next
并不重要,因为它将指向相同的位置。要测试该事物,请打印
llist
的地址和llist->next
的地址。您的llist
和llist->next
的地址相同。这意味着llist->data
和llist->next->data
是相同的。通过next
字段进行的任意数量的间接访问都是相同的。因此,在最终分配后,llist->data
为 25,之前分配的其他值将被覆盖。在最后一步你要做的:
llist->next->next->next->next = NULL;
这实际上使上图变为:
这导致
if(llist->next == NULL)
条件为真,因此仅打印第一个节点的内容,这是您插入的最后一个值 =25
要获得正确的效果,您需要分配一个新节点对于每个下一个链接,例如在代码的上下文中:
在这种情况下,图表变为
现在您可以通过执行
llist->next->next = (rame*)malloc(sizeof( struct rame));
和llist->next-next->data = 5486
等。建议不要编写
next
链可以将最后一个节点的地址临时存储在临时变量中,例如temp
并通过它们访问数据元素,例如:尽管实际上您应该将它们与具有类似以下结构的循环链接起来
您需要将列表头指针存储在某个变量中,以便您可以通过链接遍历整个列表。请注意,如果您丢失了头指针,那么您将无法获取列表。
In your code
allocates one memory location to
llist
, and the data of this location is assigned10
Next you do:
The first line assigns the
next
link of thellist
to itself, which makes the below state of the listNow executing
llist->next
points tollist
itself and thusllist->next->data
simply addresses to thelist->data
so the value 10 is changed.In the other linking you have done, how many number of times
->next->next->....->next
you use does not matter as it will point to the same location.To test the thing print the address of
llist
and the address ofllist->next
. You have address ofllist
andllist->next
identical. This meansllist->data
, andllist->next->data
are the same. and any number of indirection throughnext
field is the same. So after the final assignment thellist->data
is 25, other values previously assigned is overwritten by this.At the last step you do:
llist->next->next->next->next = NULL;
This actually makes the above diagram to:
This results in
if(llist->next == NULL)
condition to be true and thus only the first node's contents in printed, which is the last value you have inserted =25
To get the proper effect, you need to allocate a new node for each next link, like for example in the context of your code:
In this case the diagram becomes
Now you can do linking in a chain fashion by doing
llist->next->next = (rame*)malloc(sizeof(struct rame));
andllist->next-next->data = 5486
etc.Recommended is instead of writing a chain of
next
s you can store the address of the last node temporarily in a temporary variable saytemp
and access the data element through them, like:Although actually you should link these with a loop with something like the following structure
You need to store the list head pointer in some variable, so that with that you can traverse the entire list by following the links. Note that if you loose that head pointer, then you will not be able to get the list.
您将指针设置为 NULL。
You set the Pointer to NULL.
给自己画一个草图,其中包含所有分配的内存/结构和指针。然后你会看到蛇首先咬住了它的尾巴,然后它的
next
被赋值为NULL。Draw yourself a sketch containing all allocated memory / struct(s) and the pointers. Then you will see that the snake bites in its tail at first and then its
next
is assigned NULL.正在使
llist->next = NULL
因为llist->next = llist;
您需要为每个节点分配内存。对于两个节点的列表:
is making
llist->next = NULL
because ofllist->next = llist;
You need to allocated memory for each node.for a list of two nodes: