From dd036c8129334788fe8eabfeb2602c5360654b8e Mon Sep 17 00:00:00 2001 From: kunal jain Date: Thu, 21 Apr 2022 16:05:49 -0400 Subject: [PATCH] insert and remove nodes in a linked list at a given position --- linked_list_more.c | 117 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 5 deletions(-) diff --git a/linked_list_more.c b/linked_list_more.c index 9e01d5d..356da07 100644 --- a/linked_list_more.c +++ b/linked_list_more.c @@ -57,24 +57,126 @@ void printLinkedListReverse (struct NODE* last) { } } -void removeNode(struct NODE ** addr_of_first, int position) { +// Insert node at a given position in linked list +void insertNode(struct NODE ** addr_of_first, struct NODE ** addr_of_last, int position, char value) { + // addr_of_first: address of the pointer to 1st node in the list + // position: position to insert the node + // value: value to insert + + // Create node to insert using malloc + struct NODE * node_to_insert = malloc(sizeof(struct NODE)); + (*node_to_insert).value = value; + (*node_to_insert).next = NULL; + (*node_to_insert).prev = NULL; + + // Create node first, to hold value at address of first node + struct NODE * first = NULL; + first = *addr_of_first; + + // Create node current to keep track of node at current position + // Initialize current node with first node. + struct NODE * current = NULL; + current = first; + + // Create node target to keep track of node at position - 1. + // Insert node next to the target node + // Initialize target with current.prev + struct NODE * target = NULL; + target = (*current).prev; + + int i; + // Run a loop to reach the position where we want to insert + for (i = 1; i < position; i++) { + // Set target = current and + // current = current.next, until we reach insertion position + target = current; + current = (*current).next; + } + + // If target == NULL + if (target == NULL) { + // insert node at the first position + // set node_to_insert.next = current + (*node_to_insert).next = current; + (*current).prev = node_to_insert; + + // update value at address of first node + *addr_of_first = node_to_insert; + } + + // If current == NULL + else if (current == NULL) { + // insert node at last position + // set target.next = node_to_insert + (*target).next = node_to_insert; + (*node_to_insert).prev = target; + + // update value at address of last node + *addr_of_last = node_to_insert; + } + + // else + else { + // insert node between target and current node. + // set forward and backward link between + // target and node_to_insert + (*target).next = node_to_insert; + (*node_to_insert).prev = target; + // similarly set forward and backward link between + // node_to_insert and current + (*node_to_insert).next = current; + (*current).prev = node_to_insert; + } + +} + +void removeNode(struct NODE ** addr_of_first, struct NODE ** addr_of_last, int position) { + // Create a struct node. We will delete this node. struct NODE * node_to_remove = NULL; + // Initialize node_to_remove with value at address of first node node_to_remove = *addr_of_first; + + // Run a loop to reach to the position that we need to remove int i; for (i = 1; i < position; i++) { + // Reach node_to_remove position and + // set node_to_remove to node_to_remove.next node_to_remove = (*node_to_remove).next; } + + // get previous and next node of node_to_remove struct NODE * prev_node = (*node_to_remove).prev; struct NODE * next_node = (*node_to_remove).next; - (*prev_node).next = next_node; - (*next_node).prev = prev_node; + // Unlink node_to_remove. + // Reset next value of prev_node and + // prev value of next_node. + if (prev_node == NULL) { + // Reset prev value of next_node + (*next_node).prev = NULL; + + // Update value at address of first node + *addr_of_first = next_node; + } + else if (next_node == NULL) { + // Reset next value of prev_node + (*prev_node).next = next_node; + + // Update value at address of last node + *addr_of_last = prev_node; + } + else { + // Reset next value of prev_node + (*prev_node).next = next_node; + // Reset prev value of next_node + (*next_node).prev = prev_node; + } + // Release memory using free function free(node_to_remove); } - // Append new node in a linked list in much less time and computation. // Instead of going to the first element and then going through // all the elements using node.next to reach last element, @@ -148,7 +250,12 @@ int main(void) { printf("\n"); - removeNode(&first, 2); + removeNode(&first, &last, 5); + + printLinkedList(first); + printf("\n"); + + insertNode(&first, &last, 5, '7'); printLinkedList(first); printf("\n");