Delete Node at Position Linked List C

delete node at position linked list c

This tutorial’s topic is: delete node at position linked list. You will learn how to delete node at a position in a linked list. Depending on the position you give as argument, the delete operation needs to threat three cases:

  • The node you want to delete is the last node.
  • The node you want to delete is the first node.
  • The node you want to delete is a node in between the first and last.

The code below deletes a node from the list we created in the first tutorial. So if you haven’t, read here how to create your list.

Delete node at position linked list

void deleteNode(struct Person** list, int position) {
    if (*list != NULL) {
        if (position < 0) {
            return;
        } else if (position <= ((*list)->listSize - 1)) {
            if (position == ((*list)->listSize - 1)) {
                struct Person* temp = *list;
                int i = 0;
                while (temp->next) {
                    if (i == position - 1) {
                        break;
                    }
                    temp = temp->next;
                    i++;
                }
                struct Person* toBeDeleted = temp->next;
                free(toBeDeleted->name);
                free(toBeDeleted);
                temp->next = NULL;
                (*list)->listSize--;
            } else if (position == 0) {
                int size = (*list)->listSize;
                struct Person* toBeDeleted = *list;
                *list = (*list)->next;
                free(toBeDeleted->name);
                free(toBeDeleted);
                (*list)->listSize = size - 1;
            } else {
                struct Person* temp = *list;
                int i = 0;
                while (temp->next) {
                    if (i == position - 1) {
                        break;
                    }
                    temp = temp->next;
                    i++;
                }
                struct Person* toBeDeleted = temp->next;
                struct Person* next = toBeDeleted->next;
                temp->next = next;
                free(toBeDeleted->name);
                free(toBeDeleted);
                (*list)->listSize--;
            }
        } else {
            return;
        }
    } else {
        return;
    }
}

Main method for delete node at position linked list. Checking deletion of last element.

int main() {
    struct Person* list = NULL;
    list = addNode(list, "mike", 23);
    list = addNode(list, "joe", 54);
    list = addNode(list, "sam", 30);
    list = addNode(list, "john", 28);

    deleteNode(&list, 3);

    printf("List size is: %d\n", list->listSize);
    printList(list);

    return 0;
}

Output:

List size is: 3
mike, age: 23
joe, age: 54
sam, age: 30

Checking deletion of the first element:

int main() {
    struct Person* list = NULL;
    list = addNode(list, "mike", 23);
    list = addNode(list, "joe", 54);
    list = addNode(list, "sam", 30);
    list = addNode(list, "john", 28);

    deleteNode(&list, 0);

    printf("List size is: %d\n", list->listSize);
    printList(list);

    return 0;
}

Output:

List size is: 3
joe, age: 54
sam, age: 30
john, age: 28

Checking deletion of element in between:

int main() {
    struct Person* list = NULL;
    list = addNode(list, "mike", 23);
    list = addNode(list, "joe", 54);
    list = addNode(list, "sam", 30);
    list = addNode(list, "john", 28);

    deleteNode(&list, 1);

    printf("List size is: %d\n", list->listSize);
    printList(list);

    return 0;
}

Output:

List size is: 3
mike, age: 23
sam, age: 30
john, age: 28
FacebookTwitterLinkedin