c中字典实现的问题

c中字典实现的问题,c,dictionary,C,Dictionary,因此,我一直在尝试使用数组实现一个字典(仍然没有编写DiDelete函数,目前还不相关),但出现了两个问题,下面是代码: #include <stdio.h> #include <stdlib.h> #define MAX 1000 typedef int elementtype; typedef struct{ int last; elementtype elements[MAX]; }Dictionary; void DiMakeNull(Dic

因此,我一直在尝试使用数组实现一个字典(仍然没有编写DiDelete函数,目前还不相关),但出现了两个问题,下面是代码:

#include <stdio.h>
#include <stdlib.h>
#define MAX 1000

typedef int elementtype;

typedef struct{
    int last;
    elementtype elements[MAX];
}Dictionary;

void DiMakeNull(Dictionary *A)
{
    (*A).last = -1;
}

int DiMember(elementtype x, Dictionary A)
{
    int f,m,l;
    f = 0;
    l = A.last;
    m = (f+l)/2;
    while(f <= l )
    {
        if( A.elements[m] == x) return 1;
        else if( A.elements[m] < x) f = m+1;
        else l = m-1;

        m = (f+l)/2;
    }
    return 0;
}

void DiInsert(elementtype x, Dictionary *A)
{
    int i = 0,j;
    elementtype temp;
    while( ((*A).elements[i] < x) && ((*A).last >= i) )
    {
        i++;
    }
    for(j = i ; j <= (*A).last; j++)
    {
        (*A).elements[j+1] = (*A).elements[j];
    }

    (*A).elements[i] = x;
    (*A).last++;
}

int DiEmpty(Dictionary A)
{
    if(A.last == -1) return 1;
    return 0;
}

void DiPrint(Dictionary A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= A.last; i++)
        printf("%d\n",A.elements[i]);
    printf("End!");
}
我不能使用符号A->last,但我必须使用(*A).last

提前谢谢

编辑:主程序如下所示:

#include <stdio.h>
#include <stdlib.h>
#include "atp_dictionary_pomocu_liste.c"

int main()
{
    Dictionary A;
    DiMakeNull(&A);
    DiInsert(4,&A);
    DiInsert(3,&A);
    DiInsert(32,&A);
    DiPrint(A);
    return 0;
}
#包括
#包括
#包括“atp_dictionary_pomocu_liste.c”
int main()
{
字典A;
DiMakeNull(&A);
迪因塞特(4,&A);
迪因塞特(3,&A);
迪因塞特(32,&A);
DiPrint(A);
返回0;
}

我会告诉你我是如何调试的。我喜欢我的编译器,所以我编译了代码

 error: could not convert '& A' from 'Dictionary*' to 'Dictionary'
     DiPrint(&A);
               ^
它告诉我,
DiPrint()
中的类型不匹配

然后是三个变化,它起了作用

void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= (*A).last; i++)
        printf("%d\n",(*A).elements[i]);
    printf("End!");
}


我会告诉你我是如何调试的。我喜欢我的编译器,所以我编译了代码

 error: could not convert '& A' from 'Dictionary*' to 'Dictionary'
     DiPrint(&A);
               ^
它告诉我,
DiPrint()
中的类型不匹配

然后是三个变化,它起了作用

void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= (*A).last; i++)
        printf("%d\n",(*A).elements[i]);
    printf("End!");
}


您的代码中有一个错误;在
DiPrint(Dictionary A)
中,您的函数需要类型
Dictionary
,而不是类型
Dictionary*
,您需要在代码中修改此调用函数部分,因此它将是:

DiPrint(A);
void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= (*A).last; i++)
        printf("%d\n",(*A).elements[i]);
    printf("End!");
}
不是

或者另一种解决方案是修改函数以接受指针,因此:

DiPrint(A);
void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= (*A).last; i++)
        printf("%d\n",(*A).elements[i]);
    printf("End!");
}
void DiPrint(字典*A)
{
int i;
printf(“Dict:\n”);

对于(i=0;i您的代码中有一个错误;在
DiPrint(Dictionary A)
中,您的函数需要类型
Dictionary
,而不是类型
Dictionary*
,您需要在代码中修改此调用函数部分,因此它将是:

DiPrint(A);
void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= (*A).last; i++)
        printf("%d\n",(*A).elements[i]);
    printf("End!");
}
不是

或者另一种解决方案是修改函数以接受指针,因此:

DiPrint(A);
void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= (*A).last; i++)
        printf("%d\n",(*A).elements[i]);
    printf("End!");
}
void DiPrint(字典*A)
{
int i;
printf(“Dict:\n”);

对于(i=0;我没有告诉你最重要的事情-你是如何创建
字典
的实例的?@coderredoc我很抱歉,现在就把它放进去!使用所有警告和调试信息编译:
gcc-Wall-Wextra-g
。改进你的代码以避免出现警告,并使用调试器
gdb
了解hap是什么打开。也不要忘记包含.c文件不是一个好的做法。请参阅。@vasek非常感谢!我不知道!您错过了最重要的部分-您是如何创建
Dictionary
的实例的?@coderedoc我很抱歉,我会立即将其放入其中的!使用所有警告和调试信息编译:
gcc-Wall-Wextra-g
使用。改进您的代码以避免出现警告,并使用调试器
gdb
了解发生了什么。另外,不要忘记包含.c文件不是一个好做法。请参阅。@vasek非常感谢!我不知道!