2011-11-01 120 views
2

我的鏈接列表有問題。我很確定這是我的指針關閉,或者我沒有以正確的方式傳遞指針,因爲我對c是新手。結構對我來說也是新的,而C++是我習慣的語言,並且存在比我所意識到的更多的差異。我可以立即在C++中完成這個程序,但無論如何,這裏是我的代碼。鏈接列表崩潰,c

void add_process(struct process new_process, struct process *head, struct process *current){ 

    new_process.next = NULL; 

    if(head == NULL){ 
     head = &new_process; 
     current = head; 
     head->next = NULL; 
    } 
    else if(new_process.timeNeeded < head->timeNeeded){ 
     temp = head->next; 
     head = &new_process; 
     new_process.next = temp; 
    } 
    else{ 
     current = head; 
     while(new_process.timeNeeded > current->timeNeeded){ 
      temp = current; 
      current = current->next; 
     } 
     temp->next = &new_process; 
     new_process.next = current; 
    } 
} 

我正在讀取文件中的值到進程中,目前我唯一使用的是timeNeeded,它是一個int。而且我試圖按照最短時間排序。

int main(){ 
    FILE *readfile; 
    readfile = fopen("data.txt","r"); 


    head = NULL; 
    current = NULL; 

    while(fscanf(readfile, "%s %i %i %i", 
     &new_process.processName, &new_process.arrivalTime, 
      &new_process.timeNeeded, &new_process.priority) != EOF) { 

       add_process(new_process, head, current); 
     } 
    current = head; 

    while(current->next != NULL){ 
     printf("%s %i %i %i\n", new_process.processName, new_process.arrivalTime, new_process.timeNeeded, new_process.priority); 
     current = current->next; 
    } 

    return 0; 
} 

該程序崩潰在打印這不是問題。第一個問題是,我的程序每次都進入if(head == NULL)循環並在那裏插入。所以,頭可能永遠不會改變,但我不知道如何解決這個問題,我很確定它是一個雙指針,但不是正面的。而且我也確定還有其他問題,所以如果你能指出我正確的方向,並且如果我做了任何完全錯誤的事情,請告訴我。

編輯:確定後,將指針添加到頭我得到一個錯誤在head-> next = NULL說「表達式必須有指針類類型。」試圖在頭部之前添加*,但似乎沒有幫助。誰知道怎麼修它?

回答

4

你add_process功能位置:

void add_process(struct process new_process, 
       struct process *head, 
       struct process *current) 

帶您進入它的任何指針按值。這意味着在您在while循環中調用此處之後:

while(fscanf(readfile, "%s %i %i %i", 
    &new_process.processName, &new_process.arrivalTime, 
    &new_process.timeNeeded, &new_process.priority) != EOF) 
{ 

     add_process(new_process, head, current); 
} 

頭仍然是NULL,因爲它從來沒有變過。有它實際上改變頭指針,而不是其他一些指針修改add_process採取雙級指針:

void add_process(struct process new_process, 
       struct process **head, 
       struct process *current) 

與上面的代碼的另一個問題是,new_process參數採取的值也是如此。因此,這是您通過的任何流程的臨時副本。一旦add_process返回,new_process超出範圍。這意味着你的鏈表中有一個指向無效內存的懸掛指針。

要解決這個問題,你應該使用malloc動態分配內存,然後複製new_process。然後讓你的鏈表指向malloc'ed過程。使用malloc在堆上創建的對象將一直存在,直到它被釋放爲止。

這裏有一個簡單的例子來給你一個想法:

typedef struct process Process; 
void add_process(Process new_process, Process **head, Process *current) 
{ 
    Process *new_proc_copy = (Process *)malloc(sizeof(Process)); 
    // now copy over the stuff from 
    // new_process over to this one 
    memcpy((char *)new_proc_copy, (char *)new_proc, sizeof(Process)); 

    if(*head == NULL) 
    { 
     *head = new_process_copy; 
     current = *head; 
     (*head)->next = NULL; 
    } 
    else if(new_process.timeNeeded < head->timeNeeded) 
    { 
     // handle this case 
    } 
    else 
    { 
     // handle rest of your stuff 
    } 
} 

不要忘記在完成時釋放malloc分配內存。這最好在你的進程清理函數中完成 - 只有你必須手動調用它,C++中的析構函數纔是等價的。

+0

非常感謝,這一切都有道理,希望我能夠再次得到這個。 – user1019430

+0

@ user1019430記得點贊您發現有幫助的答案。 – greatwolf

+0

是的,我試過,但我需要15代表第一,當我得到它時,我會給你投票 – user1019430

0

爲了能夠改變包含在head的價值,你必須在指針傳遞給headadd_process功能。

1

你似乎沒有爲new_process分配空間。每次都不能使用相同的內存 - 必須分配一些內存。

還要記住,C不會自動更改引用參數。所以如果你想改變某些東西,你必須通過一個指向那個東西的指針。這包括其他指針 - 所以你可能需要一個指針指針。

+0

是的我想問一下,我該如何分配c中的新內存。我相信我需要使用malloc,對嗎? – user1019430

+0

是的malloc是最好的方式 – Hogan