modify student structure contents of linked list code example

Example: how to change the value of a node in a linked list in c

typedef struct node{
    int value; //this is the value the node stores
    struct node *next; //this is the node the current node points to. this is how the nodes link
}node;

node *ammendValue(node *head, int index, int val){
    node *tmp = head;
    int count = 0;

    //if the user enters an index greater then the length of the list
    if(index > len(head)){
        return NULL;
    }

    //if the last value needs editing
    if(index == -1){
        while(count < len(head)-1){
            tmp = tmp->next;
            count += 1;
        }
        tmp->value = val;
        return head;
    }

    //if the first value needs editing
    if(index == 0){
        head->value = val;
        return head;
    }

    //for any other case
    while(count < index){
        tmp = tmp->next;
        count += 1;
    }

    tmp->value = val;
    return head;
}

Tags:

C Example