1. I added the mallocing error check.
2.
or do you want the first struct added to really be, and remain the "first" node.
How can I make sure that first is really added?
3. I haved a printf("test") in order to see if addnode is called. It never gets printed so I guess something is wrong with calling the fuinction. What?
4. About const char *letter and const char *word, if i delete * it comes up with these errors: "conflicting types for addnode" and "an argument type that has a default promotion can't match an empty parameter name list declaration". What should I do?
I feel so confused with these. Correct me wherever I'm wrong.
"List" is my circularly linked list.
"First" is my first node in the list.
"newnode" is every added node.
"newnode->p_next" is my pointer to the next node.
How do I make sure that the first node points to the second one, the second to the third one... and the last to the first one?
Code:
Lics()
{
struct p_item *mylist = NULL;
char letter;
char word[SIZE];
int i, j, length;
FILE *phonetics;
char c[40];
i=0;
phonetics=fopen("ICAO.phon", "r");
if (phonetics == NULL){
printf ("Error opening file\n");
return;
}
else {
while(!feof(phonetics)) {
if (fscanf(phonetics, "%c\n", &letter) != NULL) {
if (fscanf(phonetics, "%s\n", word) != NULL) {
struct p_item *addnode(mylist, letter, word);
}
}
}
}
printf("\n\n");
printlist(mylist);
return;
}
struct p_item *addnode(struct p_item *list,const char *letter, const char *word[SIZE])
{
struct p_item *newnode, *first=list;
if (list==NULL){
list=malloc(sizeof(*list));
if (list == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
list->p_letter=letter;
strcpy(list->p_word, word);
list->p_next=first;
return first;
}
while (list->p_next != first){
list = list->p_next;
}
list = malloc(sizeof(*newnode));
if (list == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
list->p_next = newnode;
list->p_letter=letter;
strcpy(list->p_word, word);
list->p_next = list;
return list;
}