Thread: Help with a linked list.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96

    Help with a linked list.

    Hello. I'm having trouble with a simple linked list. I have two functions. One creates and populates the list. The other prints the list. For trouble shooting, I have commented out the function that prints the list and instead I've tried to print it inside the function that creates and populates it. Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    struct IRD {
        uint8_t num_region_hashes;
    } ird;
    
    typedef struct REGION {
        int val;
        struct REGION *next;
    } regions;
    
    regions *head = NULL;
    regions *ptr = NULL;
    
    void create_list(regions *head, uint8_t num_region_hashes);
    void print_list(regions *head);
    
    int main() {
        ird.num_region_hashes = 4;
    
        create_list(head,ird.num_region_hashes);
    /*
     *   print_list(&temp);
     */
    
    }
    
    void create_list(regions *head, uint8_t num_region_hashes) {
        uint8_t a = 1;
    
        head = malloc(sizeof(regions ));
    
        while(a!=num_region_hashes) {
            ptr = malloc(sizeof(regions ));
            ptr->val = a;
            printf("ptr->val = %d.\n",ptr->val);
            if(a==1) {
                head = ptr;
            }
            ptr->next = ptr;
            a++;
        }
    
        ptr->next = NULL;
    
        while(head->next != NULL) {
            printf("head->val = %d.\n",head->val);
            head = head->next;
        }
        printf("Leaving create_list.\n");
    }
    
    void print_list(regions *head) {
    
        while (head->next != NULL) {
            printf("%d\n", head->val);
            head = head->next;
        }
    
        printf("Leaving print_list.\n");
    }
    In the create_list function, when I try printing the linked list, all it does is display 1 and loop for some reason. Any ideas what I'm doing wrong? Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Essentially the code in the create_list function is a bit of a mess. You have left head pointing to itself, so your loop to print the list just keeps printing out the head value in a continuous cycle. You really need to sit down with paper and pen and go through the logic you need step by step.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Thank you for the reply. I will do that. I lost all of my programming books in a flood a while back. I've read a bit on the internet about linked lists. I think I need to find a good book (whether it's a hard copy or a digital copy) and maybe study them a bit more in depth. Thanks again for the help.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    A lot of programmers, including myself, find it helps to draw out the linked list on paper first with all the connections that need to be made and then use that as a plan to work out the necessary code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  2. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 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

Tags for this Thread