这是我的代码。我想打印所有列表数据。但我不能因为当我写while(llist->next != NULL)
llist->next
是NULL
,但我不知道为什么。请帮助我:)链接列表需要帮助不能打印我的数据。想要添加功能。在C - C++
#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;
}
嘿,我做过,但我LOOP不打印最后的数据。帮我:(
#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;
}
对ASCII艺术+1,很好的解释。 – Mat 2011-05-15 13:15:13
+1美丽的可视化! – imbaer 2011-05-15 13:15:39
@Mat,@exasm:谢谢你。特别是对于难以理解和遵循链接的初学者来说,用图像表示形象化这种链接结构是最好的。 – phoxis 2011-05-15 13:18:04