简体   繁体   中英

insert new node at any index within a single linked list

how would i go about creating a function that will allow me to insert a new node at any index within a linked list? here's the struct:

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

here's the function, note there's only a double pointer, index, and data parameter.

void insertN(struct node** headRef, int index, int data);

and here's what the result should look like after calling insertN:

[ HEAD ] -> [ 0 ] -> [ 15 ] -> [ 10 ] -> [ 5 ] -> [ NULL ]
insertN( &head, 3, -44);
[ HEAD ] -> [ 0 ] -> [ 15 ] -> [ 10 ] -> [ -44 ] -> [ 5 ] -> [ NULL ]
insertN( &head, 4, -55);
[ HEAD ] -> [ 0 ] -> [ 15 ] -> [ 10 ] -> [ -44 ] -> [-55 ] -> [ 5 ] -> [ NULL ]
insertN( &head, 0, -66);
[ HEAD ] -> [ -66 ] -> [ 0 ] -> [ 15 ] -> [ 10 ] -> [ 5 ] -> [ NULL ]

i know how to add a new node to the head, but not at any point. the way i was thinking was

void insertN(struct node** headRef, int index, int data) {
    struct node* new;
    int i;
    for (i = 0; i <= index; i++) {
        if (i == index) {
            /* move what was here to next node and put in new node */
        }
    }

    return;
}

i just am unsure how to go about doing all this, because if something was in the node, i have to move all subsequent nodes as well.

As you can see in the image below, you need to insert the node between two nodes.

The other 3 cases are

  • Inserting at the start of the list
  • Inserting in the middle of list
  • Inserting at the end of the list.

Maintain a count and loop through all the elements in the list. This count will help you keep track of the index.

Once you reach the node, where you have to insert the new node

  • Create the new node
  • Point the next pointer of the prev node to new node.
  • Point the next pointer of the new node to current node.

Full Source Code available here

在此处输入图片说明

you have to deal with one special situation when index == 0, the headRef will need to be updated. or if index is greater than the number of elements in the list. The insert will fail. Otherwise check out the example code below.

int insertN(struct node** headRef, int index, int data) {
    /* invalid headRef */
    if (!headRef) {
        return 0;
    }
    /* insert node at index */
    int i;
    struct node* new = (struct node *)malloc(sizeof(*node));
    struct node *scan = *headRef;
    new->data = data;
    new->next = 0;
    /* new head of list */
    if (scan == NULL && index == 0) {
         /* new head node */
         new->next = *headRef;
         *headRef = new;
         return 0;
    }
    /* while not at end of list and not at index */
    for (i = 0; scan != NULL && i <= index; i++) {
        if (i == index) {
            /* move what was here to next node and put in new node */
            new->next = scan->next;
            scan->next = new;
        } else {
            /* advance to next entry in list */
            scan = scan->next;
        }
    }
    /* scan == NULL indicates reached end of list before index */
    return (scan != NULL);
}

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