2016-09-26 65 views
1

請幫助我。下面的代碼是由代表多項式函數和排序是多項式:使用鏈接列表IN表示並排序多項式C

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

typedef struct PolyNode *pnode; 
typedef struct PolyNode { 
    float coef; 
    int expon; 
    pnode next; 
}; 

pnode Make_Node(pnode ptr, float coef, int expon) { 
    ptr->coef = coef; 
    ptr->expon = expon; 
    ptr->next = NULL; 
    return ptr; 
} 

pnode Input_Node(pnode ptr, float coef, int expon) { 
    if (ptr->expon < expon || ptr) { 
     pnode temp = NULL; 
     temp = malloc(sizeof(pnode)); 
     temp = Make_Node(temp, coef, expon); 
     temp->next = ptr; 
     ptr = temp; 
     return ptr; 
    } else { 
     pnode temp = NULL; 
     temp = malloc(sizeof(pnode)); 
     temp = Make_Node(temp, coef, expon); 
     pnode pol; 
     pol = ptr; 
     while (pol->next && pol->next->expon > expon) { 
      pol = pol->next; 
     } 
     temp->next = pol->next; 
     pol->next = temp; 
     return ptr; 
    } 
} 

void Print_Pol(pnode ptr) { 
    pnode temp; 
    temp = ptr; 
    while (temp) { 
     printf("%gx^%d", temp->coef, temp->expon); 
     if (temp->next != NULL) { 
      printf(" + "); 
     } 
     temp = temp->next; 
    } 
} 

int main() { 
    pnode ptr; 
    ptr = (pnode)malloc(sizeof(pnode)); 
    ptr = Make_Node(ptr, 2, 3); 
    ptr->next = NULL; 
    ptr = Input_Node(ptr, 2, 4); 
    printf("%s%d\n", &ptr, ptr->expon); 
    ptr = Input_Node(ptr, 3, 6); 
    printf("%s%d\n", &ptr, ptr->expon); 
    // ptr = Input_Node(ptr, 3, 7); 

    Print_Pol(ptr); 
    return 0; 
} 

幫幫我吧!當我在// ptr = Input_Node(ptr,3,7)之前擦除「//」時;該程序不運行。

+2

不要隱藏指針性質的typedef後面!它迷惑了每個人,包括你。 –

+0

甚至當這個評論是 – CIsForCookies

+0

它不運行我建議把錯誤輸出作爲文本在問題中;很少保證你的圖片鏈接將會持續工作。 – Jacob

回答

2

你的問題似乎是你沒有爲每個節點分配足夠的空間。鑑於此代碼:

pnode ptr; 
ptr = (pnode) malloc(sizeof(pnode)); 

pnode類型是指針類型,所以你分配用於指針足夠的空間。你需要的是一個struct PolyNode的足夠的空間,它必然大於pnode,因爲它包含其中的一個。的,而不是在一個顯式的類型方面

ptr = malloc(sizeof(*ptr)); 

關鍵的一點是,空間分配量在所期望的結果的referrent的尺寸來定義的:我建議在這種形式寫入分配。這可以防止您指定錯誤的類型,並且如果您更改指針所指向的類型,則不需要更改它。其次,您不需要在C語言中輸入malloc()的返回值(儘管您使用C++),並且您不應該這樣做。

請注意,您在代碼中存在多處錯誤分配。確保修復所有這些。

0

您沒有分配足夠的內存malloc(sizeof(nodep))sizeof(nodep)是指針的大小,而不是結構的大小。你應該改用malloc(sizeof(struct PolyNode))

考慮在Input_Node()分配內存簡化代碼:

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

typedef struct PolyNode *pnode; 
struct PolyNode { 
    float coef; 
    int expon; 
    pnode next; 
}; 

pnode Input_Node(pnode ptr, float coef, int expon) { 
    pnode temp = malloc(sizeof(*ptr)); 
    if (temp == NULL) 
     return NULL; 
    temp->coef = coef; 
    temp->expon = expon; 
    temp->next = NULL; 

    if (ptr == NULL || ptr->expon < expon) { 
     temp->next = ptr; 
     ptr = temp; 
    } else { 
     pnode pol = ptr; 
     while (pol->next && pol->next->expon > expon) { 
      pol = pol->next; 
     } 
     temp->next = pol->next; 
     pol->next = temp; 
    } 
    return ptr; 
} 

void Print_Pol(pnode ptr) { 
    pnode temp = ptr; 
    while (temp) { 
     printf("%gx^%d", temp->coef, temp->expon); 
     if (temp->next != NULL) { 
      printf(" + "); 
     } 
     temp = temp->next; 
    } 
    printf("\n"); 
} 

int main(void) { 
    pnode ptr = Input_Node(NULL, 2, 3); 
    ptr = Input_Node(ptr, 2, 4); 
    ptr = Input_Node(ptr, 3, 6); 
    ptr = Input_Node(ptr, 3, 7); 
    Print_Pol(ptr); 
    return 0; 
}