链接列表需要帮助无法打印我的数据。想做一个添加功能。在 C - C++

发布于 2024-11-07 08:05:48 字数 1910 浏览 0 评论 0原文

这是我的代码。我想打印我所有的列表数据。但我不能因为当我写 while(llist->next != NULL) llist->nextNULL 但我不'不知道为什么。请帮助我:)

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

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

发布评论

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

评论(5

趴在窗边数星星i 2024-11-14 08:05:48
llist->next = llist; 

llist 的下一个元素是 llist 本身。您本身没有链表,只有一个循环回到自身的元素。所以:

llist->next->data = 15;
llist->next->next->data = 20;
llist->next->next->next->data = 25; 

所有这些都会修改llist->data。并且:

llist->next->next->next->next = NULL;

llist->next 设置为NULL

如果您想构建列表,则需要创建新的列表元素(使用 malloc)并链接它们。例如:

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 = 15;
....

您的循环不正确:您将始终跳过最后一个条目,因为它的 ->next 将为空,因此循环体将不会运行。

尝试使用:

struct rame *cursor = llist;

while (cursor != NULL) {
  printf("%d\n", cursor->data);          
  cursor = cursor->next;
}

您使用第二个指向列表的指针,以便 llist 保持不变并指向列表标题。 (如果您不这样做,您将永远无法返回它,因为它是单链接的。)

llist->next = llist; 

llist's next element is llist itself. You don't have a linked list per se, just a single element that loops back to itself. So:

llist->next->data = 15;
llist->next->next->data = 20;
llist->next->next->next->data = 25; 

All these modify llist->data. And:

llist->next->next->next->next = NULL;

Sets llist->next to NULL.

You'll need to create new list elements (with malloc) and link them if you want to build a list. For example:

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 = 15;
....

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:

struct rame *cursor = llist;

while (cursor != NULL) {
  printf("%d\n", cursor->data);          
  cursor = cursor->next;
}

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

苍景流年 2024-11-14 08:05:48

在您的代码中

llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;

llist 分配一个内存位置,并且该位置的数据被分配 10
接下来你要做的:

llist->next = llist; 
llist->next->data = 15;

第一行将llistnext链接分配给它自己,这使得列表的状态如下

+--------+-----+------+
| llist  |  10 | next |-----+
+--------+-----+------+     |
    ^                       |
    |                       |
    +-----------------------v

现在执行llist->next< /code> 指向 llist 本身,因此 llist->next->data 只是寻址到 list->data,因此值 10 已更改。

在您完成的其他链接中,使用多少次 ->next->next->....->next 并不重要,因为它将指向相同的位置。

要测试该事物,请打印 llist 的地址和 llist->next 的地址。您的 llistllist->next 的地址相同。这意味着 llist->datallist->next->data 是相同的。通过 next 字段进行的任意数量的间接访问都是相同的。因此,在最终分配后,llist->data 为 25,之前分配的其他值将被覆盖。

在最后一步你要做的:
llist->next->next->next->next = NULL;

这实际上使上图变为:

+--------+-----+------+
| llist  |  10 | next |----->NULL
+--------+-----+------+

这导致 if(llist->next == NULL) 条件为真,因此仅打印第一个节点的内容,这是您插入的最后一个值 = 25

要获得正确的效果,您需要分配一个新节点对于每个下一个链接,例如在代码的上下文中:

llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;

llist->next = (rame*)malloc(sizeof(struct rame));  // we allocate a new location which 
                                               // we point to with the initial llist
llist->next->data = 15;                            // this will now set the data to 15 of
                                               // the node which we allocated on 
                                               // the previous step

在这种情况下,图表变为

+--------+-----+------+      +-----------------+----+------+
| llist  |  10 | next |----->| newly allocated | 15 | next |
+--------+-----+------+      +-----------------+----+------+

现在您可以通过执行 llist->next->next = (rame*)malloc(sizeof( struct rame));llist->next-next->data = 5486 等。

建议不要编写 next 链可以将最后一个节点的地址临时存储在临时变量中,例如 temp 并通过它们访问数据元素,例如:

llist = (rame*)malloc(sizeof(struct rame));
temp = llist;
temp->data = 5
temp->next = (rame*)malloc(sizeof(struct rame));
temp = temp->next; //now temp contains the address of the newly allocated node above
temp->data = 10;
temp->next = (rame*)malloc(sizeof(struct rame));
temp = temp->next;
temp->data = 15;
.
.

尽管实际上您应该将它们与具有类似以下结构的循环链接起来

list_head = (rame*)malloc(sizeof(struct rame));
temp = list->head;
while (some condition)
{
  temp->next = (rame*)malloc(sizeof(struct rame));
  temp = temp->next;
  //if this is the last node,we assign null to identify this that there is no more nodes after this.
  temp->next = NULL;
  temp->data = value;
}

您需要将列表头指针存储在某个变量中,以便您可以通过链接遍历整个列表。请注意,如果您丢失了头指针,那么您将无法获取列表。

In your code

llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;

allocates one memory location to llist , and the data of this location is assigned 10
Next you do:

llist->next = llist; 
llist->next->data = 15;

The first line assigns the next link of the llist to itself, which makes the below state of the list

+--------+-----+------+
| llist  |  10 | next |-----+
+--------+-----+------+     |
    ^                       |
    |                       |
    +-----------------------v

Now executing llist->next points to llist itself and thus llist->next->data simply addresses to the list->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 of llist->next. You have address of llist and llist->next identical. This means llist->data , and llist->next->data are the same. and any number of indirection through next field is the same. So after the final assignment the llist->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:

+--------+-----+------+
| llist  |  10 | next |----->NULL
+--------+-----+------+

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:

llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;

llist->next = (rame*)malloc(sizeof(struct rame));  // we allocate a new location which 
                                               // we point to with the initial llist
llist->next->data = 15;                            // this will now set the data to 15 of
                                               // the node which we allocated on 
                                               // the previous step

In this case the diagram becomes

+--------+-----+------+      +-----------------+----+------+
| llist  |  10 | next |----->| newly allocated | 15 | next |
+--------+-----+------+      +-----------------+----+------+

Now you can do linking in a chain fashion by doing llist->next->next = (rame*)malloc(sizeof(struct rame)); and llist->next-next->data = 5486 etc.

Recommended is instead of writing a chain of nexts you can store the address of the last node temporarily in a temporary variable say temp and access the data element through them, like:

llist = (rame*)malloc(sizeof(struct rame));
temp = llist;
temp->data = 5
temp->next = (rame*)malloc(sizeof(struct rame));
temp = temp->next; //now temp contains the address of the newly allocated node above
temp->data = 10;
temp->next = (rame*)malloc(sizeof(struct rame));
temp = temp->next;
temp->data = 15;
.
.

Although actually you should link these with a loop with something like the following structure

list_head = (rame*)malloc(sizeof(struct rame));
temp = list->head;
while (some condition)
{
  temp->next = (rame*)malloc(sizeof(struct rame));
  temp = temp->next;
  //if this is the last node,we assign null to identify this that there is no more nodes after this.
  temp->next = NULL;
  temp->data = value;
}

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.

话少情深 2024-11-14 08:05:48
llist->next->next->next->next = NULL;

您将指针设置为 NULL。

llist->next->next->next->next = NULL;

You set the Pointer to NULL.

我是男神闪亮亮 2024-11-14 08:05:48

给自己画一个草图,其中包含所有分配的内存/结构和指针。然后你会看到蛇首先咬住了它的尾巴,然后它的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.

笑叹一世浮沉 2024-11-14 08:05:48
llist->next->next->next->next = NULL;

正在使 llist->next = NULL 因为 llist->next = llist;
您需要为每个节点分配内存。对于两个节点的列表:

llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;

struct rame *llist2;
llist2 = (rame*)malloc(sizeof(struct rame));

llist2->data =15;
llist2->next = NULL;

llist->next = llist2;
llist->next->next->next->next = NULL;

is making llist->next = NULL because of llist->next = llist;
You need to allocated memory for each node.for a list of two nodes:

llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;

struct rame *llist2;
llist2 = (rame*)malloc(sizeof(struct rame));

llist2->data =15;
llist2->next = NULL;

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