简体   繁体   中英

inserting a node into a linked list

Assume structure list and node are defined as

struct list {struct node *a;};


struct node { int value;     
              struct node *next;};

The following function inserts integer e into l as the first element

void insert_first(int e, struct list *l){
   struct node * r = malloc(sizeof(struct node));
   r->value = e;
   r->next = l->a;
   l->a = r;}

Example: original list "b": 1 2 3 4

after calling insert_first(3,*b)

list "b": 3 1 2 3 4

insert_first is pretty straightfoward; however, I am having a hard time trying to figure out how to write a function insert_last which inserts a number as the last element of the list.

Example: original list "b": 1 2 3 4

after calling insert_last(3,*b)

list "b": 1 2 3 4 3

Thanks for any help in advance.

You need to save original HEAD node and traverse through list . Hope this code will help you.

struct node {
           int value;
           struct node *next;
    };

    struct list {struct node *a;};

    struct node *insert_last(int e, struct list *l) {
    /* Store the initial head of the list */
    struct list *org_head = head;
    struct node *r = malloc(sizeof(struct node));
    r->value = e;
    r->next = NULL /* Assign next pointer of current node to NULL */

    /* If the head is initially NULL, then directly return the new created node (r) as the  head of a linked list with only one node */

    if(head == NULL)
        {
            return r;
        }

    /* While we do not reach the last node, move to the next node */

      while(head -> next != NULL)
            head = head -> next;
        /* Assign the 'next' pointer of the current node to the "r" */
        head->next = r;
        /* return the original head that we have stored separately before */
        return org_head;
    }

One way of doing it is to iterate over the list until you find the tail. Something like this:

void insert_last(int e, struct list *l)
{
    // "iter" Will iterate over the list.
    struct node *iter = l->a;
    struct node *new_node = malloc(sizeof(struct node));
    // Advice: ALWAYS check, if malloc returned a pointer!
    if(!new_node) exit(1); // Memory allocation failure.
    new_node->value = e;
    new_node->next = NULL;
    if(iter){
        // Loop until we found the tail.
        // (The node with no next node)
        while(iter->next) iter = iter->next;
        // Assign the new tail.
        iter->next = new_node;
    }else{
        // The list was empty, assign the new node to be the head of the list.
        l->a = new_node;
    }
}

EDIT: Something I saw in your code, that really tickles me: ALWAYS check, when using malloc, whether you actually got a pointer back or not (check if the pointer is NULL). If malloc failes to allocate memory, be it for a lack thereof or some other critical error, it will toss you a NULL pointer. If you do not check for that, you might end up running in to some very nasty, hard to detect bugs. Just a little reminder!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM