简体   繁体   中英

Updating linked-list by pointer inside a function

I'm missing with linked-list and trying to make a function which gonna take of all the odd numbers out of the link and make a new linked-list with them.

The point is that I dont understand how to update the original list by pointer to the function, actually what I made so far is making a new list with the odd numbers but I dont really understand how to "delete" them from the original list and link all the rest togther, then send it back to the main.

Node *build_odd_list(Node *oldlst, Node *newlst) {
    Node *temp, *curheadNew;
    temp = (Node*)malloc(sizeof(Node));

    if (oldlst->value % 2 != 0) {
        temp->next = NULL;
        temp->value = oldlst->value;
        newlst = temp;
        curheadNew = newlst;
        oldlst = oldlst->next;
        printf("Passed %d\n", curheadNew->value);
    }
    else {
        oldlst = oldlst->next;
    }
    while (oldlst) {
        if (oldlst->value % 2 != 0) {
            temp = (Node*)malloc(sizeof(Node));
            temp->value = oldlst->value;
            temp->next = NULL;
            curheadNew->next = temp;
            curheadNew = curheadNew->next;
            oldlst = oldlst->next;
            printf("Passed %d\n", curheadNew->value);
        }
        else {
            oldlst = oldlst->next;
        }
    }
    return newlst;
}

Thanks a lot!

Since you need to return a new list containing the odd numbers, and modify the original list due to removal of the odd numbers, you need to pass two values back to the caller: a pointer to the first element of the updated original list, and a pointer to the first element of the "odd numbers" list.

Since you need to pass the original list to the function anyway, the simplest option for the function is to:

  • pass a pointer to a pointer to the first element of the original list;
  • modify the original list via the pointer;
  • return a pointer to the first element of the "odd numbers" list extracted from the original list.

There is no need to allocate any new elements for the "odd numbers" list as the odd number elements can be moved from one list to the other.

It is worth learning the "pointer to a pointer" trick as it is a common way of manipulating list pointers.

Here is an example program to illustrate the above method. Pay particular attention to the extract_odd_list() function and the call to that function from main() .

#include <stdio.h>
#include <stdlib.h>

typedef struct _Node {
    int value;
    struct _Node *next;
} Node;

/* Move odd numbers in *list to returned list. */
Node *extract_odd_list(Node **list) {
    Node *oddstart = NULL;      /* start of returned list */
    Node **oddend = &oddstart;  /* pointer to final link of returned list */

    while (*list) {
        if ((*list)->value % 2 != 0) {
            /* Current element of original *list is odd. */
            /* Move original *list element to end of returned list. */
            *oddend = *list;
            /* Bypass moved element in original list. */
            *list = (*list)->next;
            /* Update pointer to final link of returned list. */
            oddend = &(*oddend)->next;
        }
        else {
            /* Current element of original *list is even. */
            /* Skip to next element of original *list. */
            list = &(*list)->next;
        }
    }
    /* Terminate the returned list. */
    *oddend = NULL;
    /* And return it. */
    return oddstart;
}

void *printlist(Node *list) {
    while (list) {
        printf(" %d", list->value);
        list = list->next;
    }
}

int main(void) {
    int i;
    Node *list = NULL;
    Node *end = NULL;
    Node *oddlist;
    Node *temp;

    /* Construct a list containing odd and even numbers. */
    for (i = 1; i <= 10; i++) {
        temp = malloc(sizeof(*temp));
        temp->value = i;
        if (end == NULL) {
            list = temp;
        }
        else {
            end->next = temp;
        }
        end = temp;
    }
    end->next = NULL;
    printf("Original list:");
    printlist(list);
    printf("\n");

    /* Move the "odd number" elements from the original list to a new list. */
    oddlist = extract_odd_list(&list);

    printf("Updated list:");
    printlist(list);
    printf("\n");

    printf("Odd list:");
    printlist(oddlist);
    printf("\n");
    return 0;
}

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