2011-05-15 49 views
1

這是我的代碼。我想打印所有列表數據。但我不能因爲當我寫while(llist->next != NULL)llist->nextNULL,但我不知道爲什麼。請幫助我:)鏈接列表需要幫助不能打印我的數據。想要添加功能。在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; 
}   

回答

4

在你的代碼

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

分配一個內存位置llist,並且這個位置的數據被分配10 接下來你要做:

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

第一行分配的next鏈接llist本身,這使得列表的下面狀態

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

現在執行llist->next指向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; 
} 

您需要將表頭指針存儲在一些變量,使之與您可以遍歷整個按照鏈接列出。請注意,如果您丟失了該指針,那麼您將無法獲取該列表。

+0

對ASCII藝術+1,很好的解釋。 – Mat 2011-05-15 13:15:13

+0

+1美麗的可視化! – imbaer 2011-05-15 13:15:39

+0

@Mat,@exasm:謝謝你。特別是對於難以理解和遵循鏈接的初學者來說,用圖像表示形象化這種鏈接結構是最好的。 – phoxis 2011-05-15 13:18:04

6
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->nextNULL

如果您想建立一個列表,您需要創建新的列表元素(使用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保持不變,並在列表標題:指向與

嘗試。 (如果你不這樣做,你將永遠無法找回它,因爲它是單向鏈接。)

+0

hmm thx。但我不知道如何用malloc創建新的列表元素。你可以幫我嗎。謝謝:) – Vato 2011-05-15 12:59:27

+0

編輯我的答案,但你已經知道如何做到這一點 - 這就是你如何分配第一項:-) – Mat 2011-05-15 13:03:06

+0

+1爲了幫助你顛覆你的代表超過10K :) – ralphtheninja 2011-05-15 13:08:44

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

正在的llist->next = NULL因爲llist->next = llist; 你需要分配的內存爲每個node.for兩個節點的列表:

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; 
1
llist->next->next->next->next = NULL; 

您可以設置指針爲NULL。

0

爲自己畫一個包含所有分配的內存/結構和指針的草圖。然後你會看到蛇首先在它的尾部咬住,然後它的next被分配NULL。