Thread: Help guys with circluarly linked list

  1. #16
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    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;
    }

  2. #17
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
        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;
        }
    Where in THE ABOVE code does first get set to a value that is valid (non NULL value) to return?

    "First" is my first node in the list.
    Your above code makes this a false statement.

    Tim S.
    Last edited by stahta01; 12-15-2011 at 10:32 AM.

  3. #18
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    Treat me like a kid please. I guess I need it spelled out.

    1. So I should malloc "first"?
    2. What should i return in that piece of code?

  4. #19
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "List" is my circularly linked list.
    "First" is my first node in the list.
    In what way does your meaning of "List" and "First" differ in meaning?

    Why NOT just return "List"?

    You NEED to decide what the code variables you are using means.

    Looking at your code; changing the name of "List" to "Last" might make sense.

    What is the function addnode() supposed to be returning?

    How is mylist supposed to be updated?

    Tim S.
    Last edited by stahta01; 12-15-2011 at 10:53 AM.

  5. #20
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    Check this: circularly linked list . Why does it work for him and not for me? I took the code and modified the variable names, added the one about word and read it.

    Mine keeps giving me error "Line 54 conflicting types for 'addnode''" and "Line 44 previous implicit declaration of 'addnode' was here".

    I give you the modified code.

    If I get it right mylist is updated by "mylist = addnode(mylist, letter, word);". But why conflicting types for addnode;

    Code:
    Lics()
    {
        struct p_item *mylist = NULL;
        char letter;
        char word[SIZE];
        FILE *phonetics;
         
        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) {
                        printf("%c %s\n", letter, word);  
                        mylist = addnode(mylist, letter, word);              
                    }
                }
            }
        }
        printf("\n\n");
        printlist(mylist);
         
        return;
    }
      
    struct p_item *addnode(struct p_item *list,char letter, char word[SIZE])
    {
        struct p_item *newnode, *first=list;
        if (list==NULL){
            
            newnode=malloc(sizeof(*newnode));
            if (newnode == NULL) {
                fprintf(stderr, "Out of memory\n");
                exit(1);
            }
            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=malloc(sizeof(*newnode));    
        list->p_next = newnode;
        newnode->p_letter=letter;
        strcpy(newnode->p_word, word);
        newnode->p_next = first;
      
        return first;
    }
    Last edited by lios1984; 12-15-2011 at 01:13 PM.

  6. #21
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    Disregard "i=0;"

  7. #22
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    He put his main after the functions; you need to write function prototypes as we stated long past in this thread or move your main after the functions.

    Tim S.

    PS: I feel I am not helping you since you seem to be NOT learning; so, I am going to sign off for now.

    Quote Originally Posted by stahta01 View Post
    You need to use Function Prototypes and READ YOUR COMPILER warnings!

    http://www.cprogramming.com/tutorial/c/lesson4.html
    Tim S.
    Last edited by stahta01; 12-15-2011 at 12:23 PM.

  8. #23
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    At last!!!!!!
    Now it works. Sorry if I tired you so. I admit reading the links you gave me rather quickly and may have missed some key points. I apologize for that. I really want to thank you for your patience and for your valuable advice.

    I promise I will study more and ask less.

  9. #24
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by lios1984 View Post

    I promise I will study more and ask less.
    Asking is NOT the problem; ignoring the answers is the problem.

    FYI: Cheating on programming is like cutting off you hand because it hurts to write.

    Tim S.
    Last edited by stahta01; 12-15-2011 at 12:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Replies: 11
    Last Post: 01-02-2006, 04:46 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM