Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 双链接列表:不兼容的指针类型_C_Pointers_Struct_Doubly Linked List - Fatal编程技术网

C 双链接列表:不兼容的指针类型

C 双链接列表:不兼容的指针类型,c,pointers,struct,doubly-linked-list,C,Pointers,Struct,Doubly Linked List,目前,我正致力于C语言中平衡B树的实现。我决定使用双链表,但我遇到了一些问题。目前,我收到了第94、95和96行的警告,因为指针类型显然不兼容。 我真的不知道如何和任何帮助将是非常感激的 #include <stdio.h> #include <stdlib.h> typedef struct { int data1; int data2; int data2exists; // no: 0 , yes: 1 struct node *

目前,我正致力于C语言中平衡B树的实现。我决定使用双链表,但我遇到了一些问题。目前,我收到了第94、95和96行的警告,因为指针类型显然不兼容。 我真的不知道如何和任何帮助将是非常感激的

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

typedef struct {
    int data1;
    int data2;
    int data2exists; // no: 0 , yes: 1
    struct node * parent;
    struct node * left;
    struct node * middle;
    struct node * right;
} node;

node * insert(int *, node *, node *);
void getInput(int *);
node * createNode(int *);
void quickSwap(node *, int *, int *, int *, int *);
node * splitLeaf(int *, int *, int *, node *, node *);
void printTree(node *);

void main() {
    int input;
    getInput(&input);
    node * root = createNode(&input);
    getInput(&input);
    insert(&input, root, root); // returns current pos
    getInput(&input);
    insert(&input, root, root); // returns current pos
    getInput(&input);
    insert(&input, root, root); // returns current pos
    printTree(root);
}

node * insert(int * input, node * root, node * currentPos) {
    printf("data1: [%i], data2: [%i], d2exists: [%i], input: [%i]\n", currentPos->data1, currentPos->data2, currentPos->data2exists, *input);
    if (currentPos->left == NULL && currentPos->middle == NULL && currentPos->right == NULL) {
        // no children
        if (*input > currentPos->data1 && currentPos->data2exists == 0) {
            // data1 < input, no data2

            currentPos->data2 = *input;
            currentPos->data2exists = 1;
            return(currentPos);
            // printf("CASE1: data1 < input, no data2, no children\n");
        }
        if (*input < currentPos->data1 && currentPos->data2exists == 0) {
            // data1 > input, no data2

            currentPos->data2 = currentPos->data1;
            currentPos->data1 = *input;
            currentPos->data2exists = 1;
            return(currentPos);
            // printf("CASE2: data1 > input, no data2, no children\n");
        }
        if (currentPos->data2exists == 1) {
            // data2 exists
            int smallest;
            int middle;
            int largest;
            quickSwap(currentPos, input, &smallest, &middle, &largest);
            printf("s: [%i] m: [%i] l: [%i]\n", smallest, middle, largest);
            root = splitLeaf(&smallest, &middle, &largest, currentPos, root);
        }
    }
    return(currentPos);
}

void printTree(node * root) {
    if (root->parent != NULL) {
        printf("printTree() did not receive root!!!!\n");
        return;
    }
    else {
        printf("%i || %i", root->data1, root->data2);
        printf("\n");
        // printf("%i || %i", root->left->data1, root->left->data2);
        // printf("\t\t");
        // printf("%i || %i", root->middle->data1, root->middle->data2);
        // printf("\t\t");
        // printf("%i || %i", root->right->data1, root->right->data2);
        // printf("\n");
    }
}

node * splitLeaf(int * smallest, int * middle, int * largest, node * currentPos, node * root) {
// this function needs to return root!
    if (currentPos->parent == NULL) {
        // currentPos is root
        // create a parent with median
        node * root = createNode(middle);
        node * left = createNode(smallest);
        node * middle = createNode(largest);
        // genau hier gehts weiter! hier müssen root, left und, middle verknüpft werden!
        root->left = left;
        root->middle = middle;
        left->parent = middle->parent = root;
        // printf("root-addr: %i, left->parent: %i\n", root, left->parent);
        return(root);
    }
}

void quickSwap(node * currentPos, int * input, int * smallest, int * middle, int * largest) {
    // writes values to *smallest, *middle and *largest ordered by size

    if (currentPos->data1 > currentPos->data2) {
        *smallest = currentPos->data2;
        *middle = currentPos->data1;
    }
    else {
        *smallest = currentPos->data1;
        *middle = currentPos->data2;
    }
    if (*input < *smallest) {
        *largest = *middle;
        *middle = *smallest;
        *smallest = *input;
    }
    else if (*input < *middle) {
        *largest = *middle;
        *middle = *input;
    }
    else {
        *largest = *input;
    }
}

node * createNode(int * input) {
    node * ptr = (node*) malloc(sizeof(node));
    ptr->data1 = * input;
    ptr->data2 = 0;
    ptr->data2exists = 0;
    ptr->parent = NULL;
    ptr->left = NULL;
    ptr->middle = NULL;
    ptr->right = NULL;
    return(ptr);
}

void getInput(int * input) {
    printf("Enter a number\n");
    scanf(" %i",input);
}
#包括
#包括
类型定义结构{
int data1;
int数据2;
int data2exists;//否:0,是:1
结构节点*父节点;
结构节点*左;
结构节点*中间;
结构节点*右;
}节点;
节点*插入(int*,节点*,节点*);
void getInput(int*);
节点*创建节点(int*);
void quickSwap(node*,int*,int*,int*,int*);
node*splitLeaf(int*,int*,int*,node*,node*);
无效打印树(节点*);
void main(){
int输入;
getInput(&input);
node*root=createNode(&input);
getInput(&input);
插入(&input,root,root);//返回当前位置
getInput(&input);
插入(&input,root,root);//返回当前位置
getInput(&input);
插入(&input,root,root);//返回当前位置
打印树(根);
}
节点*插入(int*输入,节点*根,节点*当前位置){
printf(“数据1:[%i],数据2:[%i],数据2存在:[%i],输入:[%i]\n”,currentPos->data1,currentPos->data2,currentPos->data2exists,*输入);
如果(currentPos->left==NULL&¤tPos->middle==NULL&¤tPos->right==NULL){
//没有孩子
如果(*输入>当前位置->数据1和当前位置->数据2存在==0){
//数据1<输入,无数据2
currentPos->data2=*输入;
currentPos->data2exists=1;
返回(currentPos);
//printf(“案例1:data1data1&¤tPos->data2exists==0){
//数据1>输入,无数据2
currentPos->data2=currentPos->data1;
currentPos->data1=*输入;
currentPos->data2exists=1;
返回(currentPos);
//printf(“CASE2:data1>输入,没有data2,没有子项\n”);
}
如果(currentPos->data2exists==1){
//数据2存在
int最小;
中间;
int最大;
quickSwap(当前位置、输入、最小值、中间值和最大值);
printf(“s:[%i]m:[%i]l:[%i]\n”,最小、中间、最大);
root=splitLeaf(最小、中间、最大、当前位置、根);
}
}
返回(currentPos);
}
无效打印树(节点*根){
如果(根->父!=NULL){
printf(“printTree()没有收到root!!!!\n”);
返回;
}
否则{
printf(“%i | |%i”,根->数据1,根->数据2);
printf(“\n”);
//printf(“%i | |%i”,根->左->数据1,根->左->数据2);
//printf(“\t\t”);
//printf(“%i | |%i”,根->中间->数据1,根->中间->数据2);
//printf(“\t\t”);
//printf(“%i | |%i”,根->右->数据1,根->右->数据2);
//printf(“\n”);
}
}
节点*splitLeaf(int*最小,int*中间,int*最大,节点*currentPos,节点*root){
//这个函数需要返回root!
如果(currentPos->parent==NULL){
//currentPos是根
//创建具有中间带的父对象
node*root=createNode(中间);
节点*左=创建节点(最小);
节点*中间=createNode(最大);
//我的名字是韦特!我的缪森根,左下角,中下角!
根->左=左;
根->中间=中间;
左->父节点=中间->父节点=根节点;
//printf(“根地址:%i,左->父地址:%i\n”,根地址,左->父地址);
返回(根);
}
}
void quickSwap(节点*currentPos,int*输入,int*最小,int*中间,int*最大){
//按大小顺序将值写入*最小、*中间和*最大
如果(当前位置->数据1>当前位置->数据2){
*最小=当前位置->数据2;
*中间=当前位置->数据1;
}
否则{
*最小=当前位置->数据1;
*中间=当前位置->数据2;
}
如果(*输入<*最小值){
*最大=*中等;
*中等=*最小;
*最小=*输入;
}
else if(*输入<*中间){
*最大=*中等;
*中间=*输入;
}
否则{
*最大=*输入;
}
}
节点*创建节点(int*输入){
node*ptr=(node*)malloc(sizeof(node));
ptr->data1=*输入;
ptr->data2=0;
ptr->data2exists=0;
ptr->parent=NULL;
ptr->left=NULL;
ptr->middle=NULL;
ptr->right=NULL;
返回(ptr);
}
void getInput(int*input){
printf(“输入数字”);
scanf(“%i”,输入);
}

啊哈!这个问题很棘手。它与
节点的定义有关。成员
parent
left
middle
right
属于
struct node
类型,但您
typedef
直接将结构定义为
node
。我的猜测是,GCC忽略了未定义的
struct节点
,希望它是在其他地方定义的

换句话说:类型
节点
存在,但
结构节点
不存在。因此,当您尝试将
节点
分配给
结构节点
时,GCC不知道该做什么。所以改变

typedef struct {
    ...
} node;

尽管对于
结构节点
使用另一个名称可能比类型
节点
更明智

一些吹毛求疵的地方:

  • GCC抱怨
    main
    没有返回
    int
    (只是
    返回0;
  • splitLeaf
    中,将参数
    int*middle
    重新定义为
    node*middle
    ,与
    root
    相同
  • splitLeaf
    currentPos->parent
    不是
    NULL
    时不会返回任何内容(尽管您可能还没有完成该功能
    typedef struct node {
        ...
    } node;