这个程序的基本思想是实现一个通用的排序链表,从用户那里获取函数(我有一个包含这两个文件的主文件,什么都不做)。 编辑:问题是我的声明是在结构之前声明的。
我的标题:
#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
我感觉这些问题与我的结构的定义有关,但我不确定,是否会喜欢一些帮助
答案 0 :(得分:0)
您的类型定义存在问题。 从未隐藏typedef类型中的指针!
typedef void* HEAD; // DANGER!! DANGER!! DANGER!!
...
extern RESULT SLAddListElement(HEAD* head, ELM node){
Node* currNode=(Node*)head; // <= BOOM
HEAD*
表示void **
。
将指针指向某事物的head
转换为Node*
,这是指向struct的指针。这很可能是间接水平的不匹配。
即使您没有在文本编辑器中获得微小的红色东西,Visual Studio中的错误消息也应该包含一些文本和一些行信息。 您应该始终在问题中包含错误消息,以便更好地进行分析。
编辑: 缺少标题可能也是一个问题:
#include "Gadf.h" <== Probably you want gadt.h