2015-04-02 77 views
0

我是編程新手。所以這裏的問題是,當我想執行下面的程序,但它只是顯示程序已停止工作。我不知道什麼是什麼代碼是錯誤的,因爲沒有編譯錯誤。任何幫助將不勝感激。嘗試運行鏈表c程序時出錯

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct ppl 
{ 
    char name[30]; 
    struct ppl *next; 
}; 

main() 
{ 

    struct ppl *student1, *student2, *student3, *student4, *temp, *ptr, *x; 
    struct ppl *head = NULL; 
    int no; 

    head = (struct ppl *)malloc(sizeof(struct ppl)); 
    student1 = (struct ppl *)malloc(sizeof(struct ppl)); 
    student2 = (struct ppl *)malloc(sizeof(struct ppl)); 
    student3 = (struct ppl *)malloc(sizeof(struct ppl)); 
    student4 = (struct ppl *)malloc(sizeof(struct ppl)); 

    head->next = student1; 
    strcpy(student1->name, "Aizar"); 

    student1->next = student2; 
    strcpy(student2->name, "Chandi"); 

    student2->next = student3; 
    strcpy(student3->name, "Faizul"); 

    student3->next = student4; 
    strcpy(student4->name, "Joshua"); 
    student4->next = NULL; 

    ptr = head; 
    while (ptr->next != NULL) 
    { 
     ptr = (struct ppl *)malloc(sizeof(struct ppl)); 
     ptr = ptr->next; 
     printf("Name list : %s \n", ptr); 

    }; 

    return 0; 
} 
+1

您是否嘗試過使用調試器帳戶? – user4098326 2015-04-02 13:32:24

+0

我不認爲你打算在while循環內部進行分配。一旦你調用malloc(在while內部),你已經完全在列表之外完成了ptr。 – 2015-04-02 13:34:01

+0

你想從這個程序中做什麼?打印學生姓名? – MCHAppy 2015-04-02 13:35:56

回答

0

問題出在while循環中。試試這個:

while(ptr->next != NULL) 
{ 
ptr = ptr->next; 
    printf("Name list : %s \n", ptr); 

}; 

PS:ptr不需要單獨的內存分配。它將指向分配給它的指針指向的內存位置(在while循環中)。

0

注意don't give me fish teach me how to fish

一些評論:

ptr = head; // here ptr has the value of head 
    while (ptr->next != NULL) 
    { 
     ptr = (struct ppl *)malloc(sizeof(struct ppl)); // ptr loses the value of head and takes new value given by malloc 
     ptr = ptr->next; // ptr -> next is null so ptr becomes null 
     printf("Name list : %s \n", ptr); // generally if you want to print pointers use %p not %s 

    }; 

一些跡象表明:

  1. 我想你想打印學生名字不是指針。
  2. 不需要爲ptr分配內存,因爲在while循環之前它需要頭的值。

現在輪到您根據您的需求更正您的代碼。

快樂編碼:d

+0

謝謝你的幫助 – June 2015-04-03 13:20:25

0

我想你的意思了:)

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct ppl 
{ 
    char name[30]; 
    struct ppl *next; 
}; 

int main(void) 
{ 

    struct ppl *student1, *student2, *student3, *student4, *temp; 
    struct ppl *head = NULL; 

    student1 = (struct ppl *)malloc(sizeof(struct ppl)); 
    student2 = (struct ppl *)malloc(sizeof(struct ppl)); 
    student3 = (struct ppl *)malloc(sizeof(struct ppl)); 
    student4 = (struct ppl *)malloc(sizeof(struct ppl)); 

    strcpy(student1->name, "Aizar"); 
    student1->next = student2; 

    strcpy(student2->name, "Chandi"); 
    student2->next = student3; 

    strcpy(student3->name, "Faizul"); 
    student3->next = student4; 

    strcpy(student4->name, "Joshua"); 
    student4->next = NULL; 

    head = student1; 

    for (temp = head; temp != NULL; temp = temp->next) 
    { 
     printf("Student Name: %s \n", temp->name); 
    } 

    while (head) 
    { 
     temp = head; 
     head = head->next; 
     free(temp); 
    } 

    return 0; 
} 

程序輸出是

Student Name: Aizar 
Student Name: Chandi 
Student Name: Faizul 
Student Name: Joshua 

至於你的代碼,然後這個循環

while (ptr->next != NULL) 
{ 
    ptr = (struct ppl *)malloc(sizeof(struct ppl)); 
    ptr = ptr->next; 
    printf("Name list : %s \n", ptr); 

}; 

沒有意義。

此外,您不需要爲頭部分配單獨的節點。頭部應該是指向第一個分配的「學生」的指針。

考慮到,在C函數main有權返回類型int

+0

好的..謝謝你的建議 – June 2015-04-03 13:17:30