KEMBAR78
Doubly Linked List-Keynotes | PDF | Pointer (Computer Programming) | Computer Engineering
0% found this document useful (0 votes)
4 views7 pages

Doubly Linked List-Keynotes

The document provides routines for inserting and deleting nodes in a doubly linked list. It includes methods for inserting nodes at the beginning, end, and middle of the list, as well as deleting nodes from the beginning, end, and middle. Each routine is implemented in C++ and includes comments explaining the logic and operations performed.

Uploaded by

kji.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Doubly Linked List-Keynotes

The document provides routines for inserting and deleting nodes in a doubly linked list. It includes methods for inserting nodes at the beginning, end, and middle of the list, as well as deleting nodes from the beginning, end, and middle. Each routine is implemented in C++ and includes comments explaining the logic and operations performed.

Uploaded by

kji.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Doubly linked list:

1.Routine for InsertionAtBegin

Node* InsertAtBegin(Node* head, int data)


{
Node* temp = new Node(); //new allocates memory for the node structure
and temp is used to store the addr of newly allocated memory
temp->data = data;//set the data
temp->prev = nullptr; //no previous node since it’s the first
temp->next = head; //point to the current head
if (head != nullptr)// checks whether the list already has at least one node.
If head == nullptr, it means the list is currently empty, and there's no need to update
any existing node

{
head->prev = temp; // If the list is not empty, the current head node will now have
a new node (temp) inserted before it.
The head->prev pointer of the current head node is updated to point to the new node
(temp).

}
head = temp;// Assign temp (the new node) as the new head of the list.
return head;
}
2. Routine for InsertionAtEnd
Node* InsertAtEnd (Node* head, int data)
{
Node* temp = new Node();
temp->data = data;
temp->next = nullptr;
if (head == nullptr)// checks if the head pointer is nullptr, which indicates that the
list has no nodes
{
temp->prev = nullptr;// Since this new node is the only node in the list, its prev
pointer is set to nullptr

head = temp;// The head pointer is updated to point to the new node, making it the first
and only node in the list.

return head; // The function returns the updated head pointer


}
Node* last = head;
while (last->next! = nullptr) // This condition checks whether the current node
(last) has a next pointer. If next is not nullptr, it means there is another node after the
current one. This loop continues to move through the list as long as there is another node

{
last = last->next;// Inside the loop, we update the last pointer to point to the next
node (last->next). This moves the last pointer to the next node, allowing the loop to
traverse the list node by node.

}
last->next = temp;// Link the last node to the new node
temp->prev = last;// link the new node back to the last node
return head;// Return the updated head pointer
}
3.Routine for Insertion In Middle

Node* InsertAtMiddle(Node* head, int data, int position)


{
Node* temp = new Node ();
temp->data = data;
temp->prev = null;
temp->next = null;
if (position == 1 || head == null) // position == 1: If the user wants to insert the new
node at the beginning of the list (i.e., position 1).
head == nullptr: If the list is empty (head is nullptr), meaning there's no node in the list yet.

If either of these conditions is true, it means the new node should be inserted at the beginning of
the list.

{
temp->next = head; // You set temp->next to the current head of the list, which will make
temp point to the current first node (if any). If the list is empty, head will be nullptr, and temp-
>next will also be nullptr, meaning the new node becomes the first node

if (head != null)// this step updates the prev pointer of the current first node.

 If the list was not empty (head != nullptr), the previous first node's prev pointer
should now point to the new node (temp).

{
head->prev = temp;
}
head = temp;// Now that temp has been linked to the rest of the list, you update the head
pointer to point to temp, which is now the first node in the list. If the list was empty, head is now
temp

return head;
}
Node* current = head; // You initialize a pointer current that starts at the head of the list.
This will help you traverse the list until you reach the desired position.

for (int i = 1; i < position - 1 && current->next != null; i++) // This for loop
traverses the list to find the node just before the insertion position:
 i = 1: We start from the first node (head).
 i < position - 1: This ensures that we stop when we reach the node that is just before
the one where we want to insert the new node.
 current->next != nullptr: This condition ensures that you don't go beyond the end of
the list (i.e., you stop at the last node).

{
current = current->next;// Inside the loop, current = current->next; moves the pointer
current one node forward.

}
temp->next = current->next; //  After the loop, current points to the node just before
the desired insertion point (position - 1).
 This line sets the next pointer of the new node (temp) to point to the node that current-
>next is currently pointing to. If you're inserting at the end of the list, current->next will
be nullptr.

temp->prev = current; // You set the prev pointer of the new node (temp) to point to the
current node (the node just before the insertion point).

if (current->next != null)
{
current->next->prev = temp; // This accesses the prev pointer of the node that follows the
current node (the node at current->next).

}
current->next = temp; // you update the next pointer of the current node (the node before
the insertion point) to point to the new node (temp), effectively linking the new node into the list

return head;
}

2.Deletion of DLL:
1.Routine for deletion at beginning :

void del_beg_dll ()

t=head;// Step 1: Store the current head in a temporary

head=head->next; // Step 2: Update the head pointer to the next node

head->prev=NULL; //Step 3: Set the prev pointer of the new head to NULL

free(t); // Free the memory allocated for the old head node

2. Routine for deletion at end:


void del_end_dll() {
if (head == NULL) {// The first if statement checks if head is NULL, which indicates the
list is empty. If this condition is true, the function simply returns without doing anything since there
are no nodes to delete.
// The list is already empty
return;
}
if (head->next == NULL) {//  The second if statement checks if there is only one
node in the list. This is determined by checking if head->next == NULL, meaning that the
head node is also the last node (i.e., the only node in the list).

// The list has only one node

free(head);

It frees the memory occupied by head using free(head).

Sets head and tail to NULL, since the list is now empty.

head = NULL;
tail = NULL;
return;
}

// Traverse to the last node


struct Node* t = tail;
tail = tail->prev;
tail->next = NULL;
free(t);
}

3.Routine to delete at middle:


void del_mid_dll (int value)
{
t=head;// t is a pointer that starts at the head of the doubly linked list (head). This pointer
is used to traverse the list in search of the node to be deleted.

while(t->data! =value)// This loop searches through the list for a node where t->data
== value.

{
t=t->next;// If the data doesn't match value, the pointer t moves to the next node (t = t-
>next) and checks again.
 This continues until a node is found where the data matches value

}
t->prev->next=t->next; // Once the correct node is found (t), this line updates the next
pointer of the node before it (t->prev).
 It points t->prev->next to t->next, effectively bypassing the node t and linking
the previous node to the next node.

t->next->prev=t->prev; //  This line updates the prev pointer of the node after t (t-
>next).

 It points t->next->prev to t->prev, effectively bypassing the node t and linking


the next node back to the previous node

free(t);// this line deallocates the memory occupied by the node t. This is crucial to avoid
memory leaks, freeing the memory after it's no longer part of the linked list.

Delete the last node when there are multiple nodes:

// Traverse to the last node (tail)

struct Node* t = tail;

tail = tail->prev; // Move the tail pointer

tail->next = NULL; // Update the next pointer of the new tail

free(t); // Free the memory of the old tail


}

You might also like