Guys, don't even ask!! I am trying to complete a course. So far I have managed to get by regarding C. But now with malloc and linked lists, I am way over my head. What I would really appreciate is a step-by-step through my code so that I can finally realize what I am doing so wrong. Ia give you the whole code in case something's wrong with declaring or something else.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct p_item
{
char p_letter;
char p_word[15];
struct p_item *p_next;
};
typedef struct p_item p_item;
int main(int argc, char *argv[])
{
char c;
int i, j;
Lics();
printf("\n");
system("pause");
return 1;
}
Lics()
{
struct p_item *mylist = NULL;
char letter;
char word[15];
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)) {
fflush(stdin);
fscanf(phonetics, "%c", &letter);
fscanf(phonetics, "%s", word);
struct p_item *addnode(mylist, letter, word);
}
}
printf("\n\n");
printlist(mylist);
return;
}
struct p_item *addnode(struct p_item *list, char *letter, char *word[15])
{
struct p_item *newnode, *first=list;
if (list==NULL){
list=(struct p_item *)malloc(sizeof(struct p_item));
newnode->p_letter=letter;
strcpy(newnode->p_word, word);
newnode->p_next=newnode;
return newnode;
}
while (list->p_next != first){
list = list->p_next;
}
newnode = (struct p_item *)malloc(sizeof(struct p_item));
list->p_next = newnode;
newnode->p_letter=letter;
strcpy(newnode->p_word, word);
newnode->p_next = first;
return first;
}
printlist(struct p_item *list)
{
struct p_item *first = list;
if (list == NULL){
printf("The circularly linked list is empty!\n");
return;
}
do{
printf("%c %s\n", list->p_letter, list->p_word);
list = list->p_next;
} while(list != first);
}
Don't pay attention to the main() part as well as to the !feof part, for now. I mostly want to get over with the list and then the former.