So, I've been trying to implement a dictionary using an array(still haven't written DiDelete function, not relevant right now), but two problems emerged, here's the code:
#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!");
}
Question 1: why does DiPrint not work? After giving a Dictionary with confirmed numbers in its' array of elements it prints out random numbers. It seems fairly simple, I don't understand what I've gotten wrong there!
Question 2: Why is it, that when a function is e.g.
void function(Dictionary *A)
can't I use the notation A->last, but insted I have to use (*A).last
Thanks in advance!
EDIT: main program looks like this:
#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;
}