简体   繁体   English

将节点插入链表

[英]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以下函数将整数 e 作为第一个元素插入 l

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示例:原始列表“b”:1 2 3 4

after calling insert_first(3,*b)调用 insert_first(3,*b) 后

list "b": 3 1 2 3 4列表“b”:3 1 2 3 4

insert_first is pretty straightfoward; insert_first 非常简单; 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.但是,我很难弄清楚如何编写一个函数 insert_last 来插入一个数字作为列表的最后一个元素。

Example: original list "b": 1 2 3 4示例:原始列表“b”:1 2 3 4

after calling insert_last(3,*b)调用 insert_last(3,*b) 后

list "b": 1 2 3 4 3列表“b”:1 2 3 4 3

Thanks for any help in advance.提前感谢您的任何帮助。

You need to save original HEAD node and traverse through list .您需要保存原始 HEAD 节点并遍历 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).编辑:我在你的代码中看到的东西,真的让我很痒:在使用 malloc 时,总是检查你是否真的得到了一个指针(检查指针是否为 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.如果 malloc 无法分配内存,无论是由于缺少内存还是其他一些严重错误,它都会向您抛出一个 NULL 指针。 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!只是一点提醒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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