2012-09-30 110 views
0

我剛剛開始使用我的實驗室,在這裏我計算其信息存儲在結構鏈接列表中的課程的GPA。到目前爲止,我試圖打印出所有課程信息,以確保它們已被正確初始化並添加到鏈接列表中。結構鏈接列表分段 - 錯誤

我遇到了問題,但因爲我一直在收到分段錯誤。我明白分段錯誤的含義,但我不知道我犯了什麼錯誤。任何幫助,將不勝感激。

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

#define MAX_CLASSES 20 


/* Function Prototypes */ 
struct course * initcourse(int, char *, char *, float, char *, char *); 
void add(struct course *); 

/* Definition of a data node holding course information */ 
    struct course { 
    int term; 
    char name[15]; 
    char abbrev[20]; 
    float hours; 
    char grade [4]; 
    char type[12]; 
    struct course *next; 
    }; 


/* head points to first node in list, end points to last node in list */ 
/* initializes both to NULL, no nodes yet */ 
struct course *head = (struct course *) NULL; 
struct course *end = (struct course *) NULL; 


/* Initializes a node, allocates memory for the node, and returns  */ 
/* a pointer to the new node. Must pass correct parameters.   */ 
struct course * initcourse(int term, char *name, char *abbrev, float hours, char *grade, char *type) 
{ 
    struct course *ptr; 
    ptr = (struct course *) calloc(1, sizeof(struct course)); 
    if(ptr == NULL) 

    return (struct course *) NULL; 

    else 
    { 
     ptr->term = term; 
     strcpy(ptr->name, name); 
     strcpy(ptr->abbrev, abbrev); 
     ptr->hours = hours; 
     strcpy(ptr->grade, grade); 
     strcpy(ptr->type, type); 
     return ptr; 
    } 
} 


/* This adds a node to the end of the list. You must allocate a node and */ 
/* then pass its address to this function        */ 
void add(struct course *new) 
{ 
    if (head == NULL) 
    { 
     head = new; 
    } 
    else 
    { 
     end->next = new; 
     end = new; 
    } 
} 

/* Prints all information in a node */ 
void printnode(struct course *ptr) 
{ 
    printf("Term ->%d\n", ptr->term); 
    printf("Name ->%s\n", ptr->name); 
    printf("Abbreviation ->%s\n", ptr->abbrev); 
    printf("Hours ->%f\n", ptr->hours); 
    printf("Grade ->%s\n", ptr->grade); 
    printf("Type ->%s\n", ptr->type); 
} 




/* Prints List of Nodes */ 
void printlist(struct course *ptr) 
{ 
    while(ptr != NULL) 
    { 
     printnode(ptr); 
     ptr = ptr->next; 
    } 
} 

/* Calculates GPA */ 
/* float gpa (struct course *ptr) */ 
/* { */ 
/* float totalhours; */ 
/* float gpa; */ 
/* float gradepoints; */ 

/* while (ptr != NULL) */ 
/*  { */ 
/*  totalhours += (ptr->hours); */ 
/*  gradepoints = (ptr->hours * ptr->grade); */ 
/*  } */ 
/* gpa = (gradepoints /ptr->hours); */ 
/* } */ 



int main() 
{ 

    int term; 
    char name[15]; 
    char abbrev[20]; 
    float hours; 
    char grade[4]; 
    char type[12]; 
    float gpa; 
    struct course *ptr; 

    struct course course1, course2, course3; 

    course1.term = 1234; 
    strcpy(course1.name,"cse1234"); 
    strcpy(course1.abbrev,"systems"); 
    course1.hours = 4; 
    strcpy(course1.grade,"A"); 
    strcpy(course1.type,"GEC"); 


    ptr = initcourse(course1.term, course1.name, course1.abbrev, course1.hours, course1.grade, course1.type); 

    struct course *head, *ptr2; 
    head = ptr; 
    // ptr2 = ptr; 

    add(ptr); 

    course2.term = 4332; 
    strcpy(course2.name,"cse4332"); 
    strcpy(course2.abbrev,"Database"); 
    course2.hours = 4; 
    strcpy(course2.grade,"B"); 
    strcpy(course2.type,"Technical"); 

    ptr2 = initcourse(course2.term, course2.name, course2.abbrev, course2.hours, course2.grade, course2.type); 

    add(ptr2); 

    printlist(head); 



} 
+0

seg fault = **使用調試器!** –

回答

2
void add(struct course *new) 
{ 
    if (head == NULL) 
    { 
     head = new; 
    } 
    else 
    { 
     end->next = new; 
     end = new; 
    } 
} 

您需要設置endnew插入第一個節點(當head == NULL)時,否則你添加更多的節點時,提領一空指針。

而且在initcourse,你應該到next成員設置爲NULL,因爲它不是由所有位0是一個空指針表示的標準保證的(它非常可能是,但沒有保證)。

此外,

struct course *head, *ptr2; 
head = ptr; 

聲明瞭一個新的局部變量head是陰影的全球之一,而不是直接分配給head(即使是錯誤的),你應該叫add(ptr);

+0

謝謝,這解決了問題。 我有另一個快速問題(不知道我是否應該創建一個新的職位)。 我試圖用適當的等級點來定義每個字母等級。其中A = 4.0 B = 3.0 我知道使用#define語句可以實現這一點,但我無法定義+/-。例如,「A-」應該= 3.7。有沒有適當的方法來定義所有這些字母等級? 「#define」指令是否是處理此任務的正確方法?感謝您的任何幫助,我很抱歉,如果這是不正確的地方問這個問題。 -Matt –

+0

您不能'定義包含'+'或' - '的任何名稱,所以您無論如何都需要採用其他路徑。我不確定最好的方法是什麼,你有A到F的成績,每個成績都有可能的+或 - ?然後在第一個「char」上進行「switch」開關,並根據第二個開關進行修正。 –

0

您不初始化next指針,所以最後一個元素包含一個壞指針,指向一些垃圾。您也可以不初始化end

另一個問題(不涉及崩潰)是這個代碼將創建一個重複的條目:

head = ptr; 
add(ptr); 

還有一些其他的問題。你真的應該得到一個調試器,看看發生了什麼。