Hello guys, sorry for this horrible title. I have a big problem.
I created my first personal Generic Singly Linked List. This is code (in italian Lista = List):
Code:
typedef struct Node {
void *data;
struct Node *next;
}Node;
typedef Node* iterator;
typedef struct Lista {
Node *head;
Node *tail;
int length;
}Lista;
and this is function that insert an element at start:
Code:
void Insert(Lista *lista, void *data) {
Node *n = New_Node(data);
n->next = lista->head;
lista->head = n;
}
this is my New_Node function:
Code:
Node *New_Node(void *data) {
Node *n = malloc(sizeof(*n));
n->data = data;
n->next = NULL;
return n;
}
Ok, this code stayed in Lista1.c file.
Now, I need to use this code into another program, like "HashTable.c", so in HashTable.c I have insert:
Code:
#include "C:/Users/MYUSER/Documents/C/Lista1/Lista1.c"
Also in HashTable program, for testing, I imagined to have this struct:
Code:
typedef struct Contact {
char *name;
int number;
}Contact;
Everythings works fine, I can create my List and I can call Insert function (I had to remove the main from Lista1.c, only in this way all worked fine), but the problems start when I must write my Have_Name function, because I can't reach the field which I need and I don't understand why!
Code:
bool Have_Name(Lista *lista, char name[]) { // lista is not empty! I fill it with Insert function!
iterator ite = lista->head;
for (int i = 0; i < lista->length; i++) {
if (Equal(ite->data->name, name)) <--!!Problem here!!
return true;
else
ite = Next(ite);
}
return false;
}
bool Equal(char a[], char b[]) {
int i = 0;
int lengthA;
int lengthB;
while(a[i] != '\0') {
lengthA++;
i++;
}
i = 0;
while(b[i] != '\0') {
lengthB++;
i++;
}
if (lengthA != lengthB)
return false;
else
for (int i = 0; i < lengthA; i++) {
if (a[i] != b[i])
return false;
}
return true;
}
I call this function in this way:
Code:
int key = Hash_S(testName);
if (Have_Name(&array[key], testName))
printf("The list have the name!\n");
where my array of list is created in this way:
Code:
Lista *array = malloc(sizeof(*array) * 5);
for (int i = 0; i < 5; i++)
array[i] = *New_List();
Now,
Why doesn't ite->data->name return contact's name?
ite->data should return the pointer that point my "Contact" struct, and ite->data->name should return contact's name, right?
I tried to write also a cast action like java style, but without results..
Code:
((Contact) ite->data)->name
(Once again, sorry for my bad english..)
Pls help me!