gpt4 book ai didi

c - 尝试实现一个从用户那里获取功能的通用链表

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:53 26 4
gpt4 key购买 nike

这个程序的基本思想是实现一个通用的排序链表,它从用户那里获取函数(我有一个主文件,其中包含这两个文件但什么都不做)。编辑:问题是我的声明是在结构之前声明的。

我的标题:

#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

我觉得问题与我的结构定义有关,但我不确定,希望得到一些帮助

最佳答案

您的类型定义有问题。永远不要在 typedef 类型中隐藏指针!

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

...

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

HEAD* 表示 void ** 最后。您将 head 将他指向某物的指针转换为 Node* 这是指向结构的指针。这很可能是间接级别的不匹配。

即使您的文本编辑器中没有出现红色小东西,Visual Studio 的错误消息也应该包含一些文本和一些行信息。您应该始终在问题中包含错误消息,以便更好地进行分析。

编辑:缺少 header 也可能是一个问题:

#include "Gadf.h"  <== Probably you want gadt.h

关于c - 尝试实现一个从用户那里获取功能的通用链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41637454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com