這是我的代碼。我想打印所有列表數據。但我不能因爲當我寫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