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

C++ 链表需要帮助无法打印我的数据。想做一个添加函数。在C-C++;,c++,c,C++,C,这是我的密码。我想打印我所有的列表数据。但是当我写时,我不能导致(llist->next!=NULL)llist->next是NULL,但我不知道为什么。请帮帮我:) #包括 #包括 #包括 使用名称空间std; 结构框架 { int数据; 结构框架*下一步; }; int main() { 结构框架*llist; llist=(rame*)malloc(sizeof(struct-rame)); LIST->data=10; llist->next=llist; LIST->next->da

这是我的密码。我想打印我所有的列表数据。但是当我写
时,我不能导致(llist->next!=NULL)
llist->next
NULL
,但我不知道为什么。请帮帮我:)

#包括
#包括
#包括
使用名称空间std;
结构框架
{
int数据;
结构框架*下一步;
};
int main()
{ 
结构框架*llist;
llist=(rame*)malloc(sizeof(struct-rame));
LIST->data=10;
llist->next=llist;
LIST->next->data=15;
LIST->next->next->data=20;
LIST->next->next->next->data=25;
LIST->next->next->next->next=NULL;
printf(“测试”);
如果(LIST->next==NULL)
printf(“%d\n”,llist->data);
其他的
while(llist->next!=NULL)
{
printf(“%d\n”,llist->data);
llist=llist->next;
} 
系统(“暂停”);
返回0;
}  
嘿,我做了所有的事情,但是我的循环没有打印最后的数据。帮助我:(

#包括
#包括
#包括
使用名称空间std;
结构框架
{
int数据;
结构框架*下一步;
};
int main()
{ 
结构框架*llist;
llist=(rame*)malloc(sizeof(struct-rame));
LIST->data=10;
llist->next=(rame*)malloc(sizeof(struct-rame));
LIST->next->data=15;
llist->next->next=(rame*)malloc(sizeof(struct-rame));
LIST->next->next->data=20;
llist->next->next->next=(rame*)malloc(sizeof(struct rame));
LIST->next->next->next->data=25;
llist->next->next->next->next=(rame*)malloc(sizeof(struct rame));
LIST->next->next->next->next=NULL;
printf(“测试”);
while(llist->next!=NULL)
{
printf(“%d\n”,llist->data);
llist=llist->next;
} 
系统(“暂停”);
返回0;
}          
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
的下一个元素是
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 = (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=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;
将指针设置为NULL


您将指针设置为NULL。

绘制一个包含所有分配内存/结构和指针的草图。然后您将看到蛇先咬它的尾巴,然后它的
下一个
被分配为NULL。

绘制一个包含所有分配内存/结构的草图然后你会看到蛇先咬它的尾巴,然后它的
下一个
被赋值为空。

在你的代码中

llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
将一个内存位置分配给
llist
,并将此位置的数据分配给
10
接下来你要做:

llist->next = llist; 
llist->next->data = 15;
第一行将
llist
next
链接分配给自身,从而使列表处于以下状态

+--------+-----+------+
| llist  |  10 | next |-----+
+--------+-----+------+     |
    ^                       |
    |                       |
    +-----------------------v
现在执行
llist->next
指向
llist
本身,因此
llist->next->data
只是指向
list->data
,因此值10被更改

在您完成的另一个链接中,您使用
->next->next->next->..->next
的次数并不重要,因为它将指向相同的位置

要测试这个东西,请打印
llist
的地址和
llist->next
的地址。您有
llist
llist->next
的地址。这意味着
llist->data
llist->next->data
的地址是相同的。通过
next
字段的任何间接寻址数都是相同的。因此最终赋值后,
llist->data
为25,之前赋值的其他值将被该值覆盖

在最后一步中,您需要执行以下操作:
llist->next->next->next->next->next=NULL;

这实际上使上述图表:

+--------+-----+------+
| llist  |  10 | next |----->NULL
+--------+-----+------+
这将导致
if(llist->next==NULL)
条件为true,因此仅打印第一个节点的内容,这是您插入的最后一个值=
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->data=5以链式方式进行链接
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;
}