This is a pretty long answer. Do not take it personally, but you have made quite a few rookie mistakes. I met with many people at the university, in which I helped to learn the C language and programming in general, so I used to note such things.
Important issues I could find
words
, , , . : .
append
. , . append. , head tmp, tmp NULL, NULL, head NULL. , node NULL. NULL, .
create_list
, . ( current ) , . append head, . , current. ( head.)
print_list
, node. . ( .) return void .
,
@Baltasarq clear , .:)
,
void* char*
, data NODE , void*? ! ( , .)
new ++. , .
, -
: print_list (, tmp append), head. ( NODE .)
( , , , .)
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct node
{
void *data;
struct node *next;
} NODE;
NODE *new_node(void *data)
{
NODE *newNode = (NODE*)malloc(sizeof(NODE));
if (newNode)
{
newNode->data = data;
newNode->next = NULL;
return newNode;
}
return NULL;
}
void append(NODE *head, NODE *node)
{
if (head && node)
{
NODE *tmp = head;
while (tmp->next)
tmp = tmp->next;
tmp->next = node;
}
}
void print_list(NODE *node, void (print_fn) (void*))
{
while (node)
{
if (print_fn)
print_fn(node->data);
else
printf("Word: %s\n", (char *)node->data);
node = node->next;
}
}
NODE *create_list()
{
FILE *dict_file = fopen("trial.txt", "r");
if (dict_file)
{
NODE *head = NULL;
NODE *current = head;
char word[20];
memset(word, '\0', 20);
while (fgets(word, sizeof(word), dict_file))
{
char *data = calloc(sizeof(word) + 1, sizeof(char));
strcpy(data, word);
append(current, new_node((void*)data));
if (current->next)
current = current->next
}
fclose(dict_file);
return head;
}
else
{
printf("ERROR: File not found");
}
return NULL;
}
int main(int argc, char *argv[])
{
NODE *head = create_list();
if (!head)
{
printf("ERROR: Either malloc() failed or data not found\n");
}
else
{
print_list(head, NULL);
}
return 0;
}