2017-01-13 5 views
-1

このプログラムの基本的な考え方は、ユーザーから関数を取得する汎用ソートリンクリストを実装することです(これらの2つのファイルを含むメインファイルはありません) 。 編集:問題は私の宣言が構造体の前に宣言されていたことでした。ユーザーから関数を取得する一般的なリンクリストを実装しようとする

私のヘッダー:

#ifndef GADT_H 
#define GADT_H 
#include <stdio.h> 
//typedef declaration 
typedef void* ELM; 
typedef void* SLN; 
typedef void* HEAD; 
typedef enum { success, outOfMem, badArgs, failure} RESULT; 

HEAD SLCreate(ELM head_val, ELM(*create_elm)(), void(*cpy_elm)(ELM, ELM), 
int(*cmp_elm)(ELM, ELM), void(*free_elm)(ELM), 
void(*print_elm)(ELM), ELM(*add_elm_to_elm)(ELM, ELM)); 
void SLDestroy(HEAD head); 
RESULT SLAddListElement(HEAD* head, ELM node); 
RESULT SLRemoveElement(HEAD* head, ELM node); 
SLN SLFindElement(HEAD head, ELM node); 
void SLAddToElement(HEAD* head, ELM toEl, ELM addEl); 
void SLPrintElements(HEAD head); 
#endif 

私のCファイル:

#include <stdio.h> 
#include <stdlib.h> 
#include <malloc.h> 
#include "gadt.h" 
//static funcs: 
static void sortLinkedList(HEAD* head); 
static void nodeDataSwap(Node* nodeOne, Node* nodeTwo); 
typedef struct Node{ 
    ELM data; 
    struct Node * next; 
    //Functions associated with the struct. 
    ELM(*create_elm)(); 
    void(*cpy_elm)(ELM, ELM); 
    int(*cmp_elm)(ELM, ELM); 
    void(*free_elm)(ELM); 
    void(*print_elm)(ELM); 
    ELM(*add_elm_to_elm)(ELM, ELM); 
}Node; 
/************************************************************************ 
* function name: SLCreate 
* The input: a pointer to a value(void*), pointers to functions 
    that are associated with the struct. 
* The output: void* 
* the operation: creates the a pointer to a new linked list 
and initializes its functions. 
*************************************************************************/ 
extern HEAD SLCreate(ELM head_val, ELM(*create_elm)(), void(*cpy_elm)(ELM, ELM), 
int(*cmp_elm)(ELM, ELM), void(*free_elm)(ELM), 
void(*print_elm)(ELM), ELM(*add_elm_to_elm)(ELM, ELM)){ 
    HEAD head; 
    Node* pToFirstNode=(Node*)malloc(sizeof(Node)); 
    if (pToFirstNode!=NULL){ //if the allocation hasn't failed 
     pToFirstNode->data=(ELM)create_elm(); 
     cpy_elm(pToFirstNode->data, head_val); 
     pToFirstNode->next=NULL; 
     //initializing functions assosiated with Head. 
     pToFirstNode->cpy_elm=cpy_elm; 
     pToFirstNode->create_elm=create_elm; 
     pToFirstNode->cmp_elm=cmp_elm; 
     pToFirstNode->free_elm=free_elm; 
     pToFirstNode->print_elm=print_elm; 
     pToFirstNode->add_elm_to_elm=add_elm_to_elm; 
     head=pToFirstNode; //so both of them point ot the same place 
     return head; 
    } 
    return; 
} 
/************************************************************************ 
* function name: SLAddListElement 
* The input: the head of the list and the node to be created 
* The output: integer-RESULT (outOfMem, Succes) 
* the operation:goes to the end of the linked list, allocates memory at the end 
and copies node to it. 
*************************************************************************/ 
extern RESULT SLAddListElement(HEAD* head, ELM node){ 
    Node* currNode=(Node*)(*head); 
    Node* lastNode=(Node*)(*head); 
    while(currNode->next!=NULL){ 
     lastNode=currNode; 
     currNode=currNode->next; 
    } 
    currNode=(Node*)malloc(sizeof(Node)); 
    if(currNode==NULL){ 
     return outOfMem; 
    } 
    //inserting functions into currNode 
    currNode->add_elm_to_elm=lastNode->add_elm_to_elm; 
    currNode->cpy_elm=lastNode->cpy_elm; 
    currNode->create_elm=lastNode->create_elm; 
    currNode->free_elm=lastNode->free_elm; 
    currNode->print_elm=lastNode->print_elm; 
    currNode->cmp_elm=lastNode->cmp_elm; 
    //inserting data into currNode 
    currNode->cpy_elm(currNode->data,node);//calls the function from head. 
    currNode->next=NULL; 
    //sort the list 
    sortLinkedList(head); 

    return success; 
} 

extern void SLDestroy(HEAD head){ 
    Node* currNode=(Node*)head; 
    Node* subsequentNode=(Node*)head; //next node after previous 
    //need two pointers because after i free the first one 
    //i dont have access to the next node. 
    while(currNode->next!=NULL){ 
     subsequentNode=subsequentNode->next; 
     currNode->free_elm(currNode->data); 
     free(currNode); 
     currNode=subsequentNode; 
    } 
} 
static void sortLinkedList(HEAD* head){ 
    Node* currNode=(Node*)(*head); 
    while(currNode->next=NULL); 
    if(currNode->cmp_elm(currNode->data,currNode->next->data) > 0){ 
     nodeDataSwap(currNode,currNode->next); 
    } 
} 
static void nodeDataSwap(Node* nodeOne, Node* nodeTwo){ 
    void * temp; 
    temp=nodeOne->data; 
    nodeOne->data=nodeTwo->data; 
    nodeTwo->data=temp; 
} 

extern void SLPrintElements(HEAD head){ 
    Node* nodePointer=(Node*)head; 
    nodePointer->print_elm(nodePointer->data); 
    while (nodePointer=NULL){ 
     printf("\n\t"); 
     nodePointer->print_elm(nodePointer->data); 
    } 
} 

これらは私が取得エラーです:PICTURE

私は問題が私の定義に接続されている感を持っています構造体ですが、わかりませんが、いくつかの助けが大好きです

+2

「私は、構文エラーのトンを得る」 - >これらは、間違っているものを決定する際にあなたに便利です。これらのエラーのテキストを含めると、あまりにも役に立ちます。それらをここに追加することを提案してください。 – chux

+0

**決して** typedefポインタはありません。また、 'void *'をあまり使わないでください。あなたのコードは非常にエラーを起こしやすく、読みにくいです。自己文書化の名前、大文字の名前はマクロのみ、 'enum'定数とバイナリ演算子のまわりのスペースを使用してください。そして、[尋ねる]私たちは教習やデバッグのサービスをしていません。 – Olaf

+0

あなたの問題には関係しませんが、 'malloc()'の戻り値を_not_キャストするべきではありません。関数ポインタ引数にtypedefを使用すると、読みやすくなります。 – Gerhardh

答えて

0

あなたはprあなたのタイプ定義を持ってください。 これまで決して typedef'dタイプのポインタを隠してください!

typedef void* HEAD; // DANGER!! DANGER!! DANGER!! 

... 

extern RESULT SLAddListElement(HEAD* head, ELM node){ 
    Node* currNode=(Node*)head; // <= BOOM 

HEAD* THTエンドでvoid **を意味します。 headのポインタをポインタへのポインタに変換すると、Node*がstructのポインタになります。これは、間接化のレベルのミスマッチである可能性が非常に高いです。

テキストエディタに小さな赤い物がない場合でも、Visual Studioのエラーメッセージにはテキストと行情報が含まれている必要があります。 より良い分析を可能にするために、常に質問にエラーメッセージを含める必要があります。

編集: 見つからないヘッダも問題になるかもしれません:

#include "Gadf.h" <== Probably you want gadt.h 
関連する問題