C 如何在LinkedList的末尾插入值?

C 如何在LinkedList的末尾插入值?,c,linked-list,C,Linked List,我正在尝试向LinkedList的末尾添加给定的值。我知道如何迭代到LinkedList的末尾,但我不确定从那里开始该怎么做 void llist_insert_last(LinkedList * list, int value) { ListNode * e = list->head; while(e != NULL) { e = e->next; } } 如果要进行堆栈,则在链表末尾插入值如下所示: ListNode *prev = list->he

我正在尝试向LinkedList的末尾添加给定的值。我知道如何迭代到LinkedList的末尾,但我不确定从那里开始该怎么做

void llist_insert_last(LinkedList * list, int value) {
  ListNode * e = list->head;
  while(e != NULL) {
    e = e->next;
  }
}

如果要进行堆栈,则在链表末尾插入值如下所示:

 ListNode *prev = list->head;  
 /* Make sure to check the return value of malloc in real life */
 ListNode *curr = malloc(sizeof(LinkedNode));
 curr->data = value;
 curr->next = prev;
您需要向指针传递一个指针,以修改
list

但是,如果正在执行队列,要迭代到列表的末尾,应执行以下操作:

ListNode * e = list->head;
while(e->next != NULL) e = e->next;

/* set E to a ListNode */

如果要进行堆栈,则在链表末尾插入值如下所示:

 ListNode *prev = list->head;  
 /* Make sure to check the return value of malloc in real life */
 ListNode *curr = malloc(sizeof(LinkedNode));
 curr->data = value;
 curr->next = prev;
您需要向指针传递一个指针,以修改
list

但是,如果正在执行队列,要迭代到列表的末尾,应执行以下操作:

ListNode * e = list->head;
while(e->next != NULL) e = e->next;

/* set E to a ListNode */

如果要进行堆栈,则在链表末尾插入值如下所示:

 ListNode *prev = list->head;  
 /* Make sure to check the return value of malloc in real life */
 ListNode *curr = malloc(sizeof(LinkedNode));
 curr->data = value;
 curr->next = prev;
您需要向指针传递一个指针,以修改
list

但是,如果正在执行队列,要迭代到列表的末尾,应执行以下操作:

ListNode * e = list->head;
while(e->next != NULL) e = e->next;

/* set E to a ListNode */

如果要进行堆栈,则在链表末尾插入值如下所示:

 ListNode *prev = list->head;  
 /* Make sure to check the return value of malloc in real life */
 ListNode *curr = malloc(sizeof(LinkedNode));
 curr->data = value;
 curr->next = prev;
您需要向指针传递一个指针,以修改
list

但是,如果正在执行队列,要迭代到列表的末尾,应执行以下操作:

ListNode * e = list->head;
while(e->next != NULL) e = e->next;

/* set E to a ListNode */

迭代到列表末尾的循环是好的,但是它走得太远了,指针以NULL结尾。这使得它变得无用,因为它不再指向有效的列表节点

您需要找到最后一个元素,即伪代码,例如:

def appendNode (list, payload):
    // Create the new node with payload.

    node = new node()
    if node == NULL:
        handleOutOfMemoryIntelligently()
    node.payload = payload
    node.next = NULL

    // Handle special case of empty list,
    //   needs pass by reference for list

    if list == NULL:
        list = node
        return

    // Find the last item in the list (the one that
    //   has a NULL next pointer) and adjust it to
    //   point to the new node.

    while list.next != NULL:
        list = list.next
    list.next = node

迭代到列表末尾的循环是好的,但是它走得太远了,指针以NULL结尾。这使得它变得无用,因为它不再指向有效的列表节点

您需要找到最后一个元素,即伪代码,例如:

def appendNode (list, payload):
    // Create the new node with payload.

    node = new node()
    if node == NULL:
        handleOutOfMemoryIntelligently()
    node.payload = payload
    node.next = NULL

    // Handle special case of empty list,
    //   needs pass by reference for list

    if list == NULL:
        list = node
        return

    // Find the last item in the list (the one that
    //   has a NULL next pointer) and adjust it to
    //   point to the new node.

    while list.next != NULL:
        list = list.next
    list.next = node

迭代到列表末尾的循环是好的,但是它走得太远了,指针以NULL结尾。这使得它变得无用,因为它不再指向有效的列表节点

您需要找到最后一个元素,即伪代码,例如:

def appendNode (list, payload):
    // Create the new node with payload.

    node = new node()
    if node == NULL:
        handleOutOfMemoryIntelligently()
    node.payload = payload
    node.next = NULL

    // Handle special case of empty list,
    //   needs pass by reference for list

    if list == NULL:
        list = node
        return

    // Find the last item in the list (the one that
    //   has a NULL next pointer) and adjust it to
    //   point to the new node.

    while list.next != NULL:
        list = list.next
    list.next = node

迭代到列表末尾的循环是好的,但是它走得太远了,指针以NULL结尾。这使得它变得无用,因为它不再指向有效的列表节点

您需要找到最后一个元素,即伪代码,例如:

def appendNode (list, payload):
    // Create the new node with payload.

    node = new node()
    if node == NULL:
        handleOutOfMemoryIntelligently()
    node.payload = payload
    node.next = NULL

    // Handle special case of empty list,
    //   needs pass by reference for list

    if list == NULL:
        list = node
        return

    // Find the last item in the list (the one that
    //   has a NULL next pointer) and adjust it to
    //   point to the new node.

    while list.next != NULL:
        list = list.next
    list.next = node

当然有几种解决方案,但我最喜欢的是双间接解决方案。它使用一个额外的间接级别,使头指针和下一个指针可以对称访问:

void llist_insert_last(LinkedList * list, int value)
{
    ListNode ** e = &list->head;
    while((*e) != NULL) {
        e = &(*e)->next;
    }
    *e = llist_node_allocate(value);  //need implementation details.
    *e->next = NULL;
}

这是一个很好的教学练习,对于那些试图在指针方面做得更好的人来说:)

当然有几种解决方案,但我最喜欢的是双间接解决方案。它使用一个额外的间接级别,使头指针和下一个指针可以对称访问:

void llist_insert_last(LinkedList * list, int value)
{
    ListNode ** e = &list->head;
    while((*e) != NULL) {
        e = &(*e)->next;
    }
    *e = llist_node_allocate(value);  //need implementation details.
    *e->next = NULL;
}

这是一个很好的教学练习,对于那些试图在指针方面做得更好的人来说:)

当然有几种解决方案,但我最喜欢的是双间接解决方案。它使用一个额外的间接级别,使头指针和下一个指针可以对称访问:

void llist_insert_last(LinkedList * list, int value)
{
    ListNode ** e = &list->head;
    while((*e) != NULL) {
        e = &(*e)->next;
    }
    *e = llist_node_allocate(value);  //need implementation details.
    *e->next = NULL;
}

这是一个很好的教学练习,对于那些试图在指针方面做得更好的人来说:)

当然有几种解决方案,但我最喜欢的是双间接解决方案。它使用一个额外的间接级别,使头指针和下一个指针可以对称访问:

void llist_insert_last(LinkedList * list, int value)
{
    ListNode ** e = &list->head;
    while((*e) != NULL) {
        e = &(*e)->next;
    }
    *e = llist_node_allocate(value);  //need implementation details.
    *e->next = NULL;
}

这是一个很好的教学练习,适用于试图在指针方面做得更好的人:)

告诉我们什么是
LinkedNode
LinkedList
看起来像是你需要为新节点分配内存,将值放入节点,然后将
e->next
指向节点。告诉我们
LinkedNode
是什么,
LinkedList
看起来您需要为新节点分配内存,将值放入节点,然后将
e->next
指向节点。向我们展示什么
LinkedNode
LinkedList
看起来您需要为新节点分配内存,将值放入节点,然后将
e->next
指向节点。向我们展示
LinkedNode
LinkedList
的外观您需要为新节点分配内存,将值放入节点,然后将
e->next
指向节点。