Skip to content

Commit

Permalink
insert and remove nodes in a linked list at a given position
Browse files Browse the repository at this point in the history
  • Loading branch information
jainkhere committed Apr 21, 2022
1 parent e72598a commit dd036c8
Showing 1 changed file with 112 additions and 5 deletions.
117 changes: 112 additions & 5 deletions linked_list_more.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit dd036c8

Please sign in to comment.