1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>

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

void insert_after(struct Node* existing, int value, struct Node** a_head) {
    if( *a_head == NULL ) {              // empty list
        *a_head = malloc(sizeof(**a_head));
        (*a_head) -> value = value;
        (*a_head) -> next  = NULL;
    }
    else if(existing == NULL) {           // insert at head
        struct Node* new_node = malloc(sizeof(*new_node));
        new_node -> value = value;
        new_node -> next  = *a_head;
    }
    else if(existing -> next == NULL) {   // insert at tail
        existing -> next = malloc(sizeof(*(existing -> next)));
        existing -> next -> value = value;
        existing -> next -> next  = NULL;
    }
    else {                                // insert in middle
        struct Node* new_node = malloc(sizeof(*new_node));
        new_node -> value = value;
        new_node -> next  = existing -> next;
        existing -> next  = new_node;
    }
}

int main(int argc, const char* argv[]) {

    return EXIT_SUCCESS;
}

This content is protected and may not be shared, uploaded, or distributed.    © 2019 Alexander J. Quinn.