Thread: Help figuring out Linked List insertion

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    7

    Help figuring out Linked List insertion

    I'm trying to figure out how to insert a node into a linked list at a particular location based on a time and am confused. Any suggestions or help would be appreciated.

    I have this declared outside of everything globally.

    Code:
    struct aTime {    
        char        name[LENGTH];
        int        time;
        struct aTime* next;
    };
    
    
    struct aTime* head = NULL;
    And then this function that is used to add nodes (aTime structs) to a linked list. It adds the first node fine, but not subsequent ones. I think this is because I have while p-> != NULL but it is always going to be null when the function is called since I create a new aTime struct. So I guess my question is how, after one struct has been added at the beginning, do I point to the head node and traverse the list, insert a node, and make sure everything is still linked? Do I need another temp aTime struct?

    Code:
    void add_time(char name[LENGTH], int seconds)
    {
        struct aTime *p;
        p = (struct aTime *) malloc(sizeof(struct aTime));
    
    
        if (head == NULL)
        {    
            strcpy(p->name, name);
            p->seconds = seconds;
            p->next = list_head;
            list_head = p;
            return;
        }
        else
        {
            while (p->next != NULL)
            {
                if (p->seconds < seconds)
                {
                    strcpy(p->name, name);
                    p->seconds = seconds;
    
    
                    return;
                }
                else
                {
                    p = p->next;
                }
            }
        }
    }
    Thanks for any help or suggestions.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    After the else, p->next is used but never initialized, and it's not yet part of the linked list. You need to use a temp pointer to atime, initialize it temp = head, and advance that temp pointer to the insertion spot.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    7
    Quote Originally Posted by rcgldr View Post
    After the else, p->next is used but never initialized, and it's not yet part of the linked list. You need to use a temp pointer to atime, initialize it temp = head, and advance that temp pointer to the insertion spot.
    Thanks for the suggestions.

    So if I make a
    Code:
    struct aTime *temp = list_head;
    Do I just need to use that instead of p after the else? Since it is pointing at the head of the linked list and then traverses through it?

    Hrm..I found a few examples online that don't really look much like what I have so I guess I'll keep working at it.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    14
    First initialize a temp and temp1 pointer which is equal to head pointer just before while loop
    Code:
     temp=head;
    Code:
    while(temp->next != NULL)
    {
      temp1=temp;
      temp=temp->next;
    }
    if(p->secounds < secounds)
    {
      p->next=temp;
      temp1->next=p;
    }
    else
    {
      temp->next=p;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP! Linked List Insertion
    By divisionbyzero in forum C Programming
    Replies: 1
    Last Post: 04-08-2010, 03:43 PM
  2. Alphabetical Linked List Insertion
    By serg_yegi in forum C Programming
    Replies: 6
    Last Post: 03-27-2010, 11:11 PM
  3. insertion in a sorted linked list
    By sagsriv in forum C Programming
    Replies: 4
    Last Post: 03-15-2008, 02:40 PM
  4. Linked List alpha insertion
    By mouse163 in forum C++ Programming
    Replies: 9
    Last Post: 02-21-2005, 02:24 PM
  5. Insertion Linked List Help
    By 0rion in forum C Programming
    Replies: 4
    Last Post: 05-12-2004, 07:38 AM