Insertion
Inserting elements at different positions in a doubly linked list, including at the beginning, end, and at specific positions
Insert At Beginning
Create a New Node:
-
Allocate memory for a new node.
-
Set the
datafield of the new node to the given value. -
Initialize the
prevandnextfields of the new node toNULL.
Update the Next Pointer
- Point the
nextfield of the new node to the current head node (head).
Update the Prev Pointer of the Current Head (if it exists)
- If the current
headis notNULL, set theprevfield of the current head node to the new node.
Change the Head
-
Set the
headof the list to the new node. The new node becomes the first node in the list. -
The
headpointer now points to the newly added node.
// insertatbeginning.cpp
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
Node(int d) {
data = d;
prev = next = NULL;
}
};
Node* insertBegin(Node* head, int data) {
Node* newNode = new Node(data);
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
}
return newNode;
}
void printList(Node* head) {
Node* curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
cout << "\n";
}
int main() {
Node* head = new Node(2);
Node* second = new Node(3);
Node* third = new Node(4);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Original Linked List: ";
printList(head);
head = insertBegin(head, 1);
cout << "After inserting at beginning: ";
printList(head);
return 0;
}print("We're Working")console.log("We're Working");Insert At Position
Create a New Node
-
Allocate memory for a new node.
-
Set the
datafield of the new node tonew_data. -
Initialize the
prevandnextfields of the new node toNULL.
Check for Insertion at the Beginning
-
If
pos == 1: -
Set the
nextfield of the new node to the current head. -
If the head is not
NULL, set theprevfield of the current head to the new node. -
Update the head to point to the new node.
-
Return the updated head.
Traverse to the Desired Position
-
Initialize a pointer
currto the head. -
Iterate through the list for
pos - 1steps or untilcurrbecomesNULL.
Check for Valid Position
- If
currisNULLbefore reaching the desired position, print an error message and exit the function. The position is out of bounds.
Insert the New Node
-
Set the
prevfield of the new node tocurr. -
Set the
nextfield of the new node tocurr->next. -
Update
curr->nextto point to the new node. -
If the new node is not being inserted at the end, update the
prevfield of the node following the new node to point to the new node.
Return the Updated Head
- Return the head pointer after insertion.
//inseratposition.cpp
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next, *prev;
Node(int new_data) {
data = new_data;
next = prev = nullptr;
}
};
Node *insertAtPosition(Node *head, int pos, int new_data) {
Node *newNode = new Node(new_data);
if (pos == 1) {
newNode->next = head;
if (head != NULL)
head->prev = newNode;
head = newNode;
return head;
}
Node *curr = head;
for (int i = 1; i < pos - 1 && curr != NULL; ++i) {
curr = curr->next;
}
if (curr == NULL) {
cout << "Position is out of bounds." << endl;
delete newNode;
return head;
}
newNode->prev = curr;
newNode->next = curr->next;
curr->next = newNode;
if (newNode->next != NULL)
newNode->next->prev = newNode;
return head;
}
void display(Node *head) {
Node *curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
Node *head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(4);
head->next->next->prev = head->next;
cout << "Original Linked List: ";
display(head);
cout << "Inserting 3 at position 3: ";
int data = 3;
int pos = 3;
head = insertAtPosition(head, pos, data);
display(head);
return 0;
}print("We're Working")console.log("We're Working");Insertion At Tail
Create a New Node
-
Allocate memory for the new node.
-
Set the
datafield of the new node tonew_data. -
Initialize the
nextandprevfields of the new node toNULL.
Check if the List is Empty
-
If
head == NULL, set theheadpointer to the new node (as it will be the only node in the list). -
Return the updated
head.
Traverse to the End of the List
- Initialize a pointer
currto the head of the list. - Move
currthrough the list by following thenextpointer untilcurr->next == NULL.
Insert the New Node at the End
- Set the
nextpointer of the last node (curr) to the new node. - Set the
prevpointer of the new node tocurr.
Return the Updated Head
- Return the
headpointer after insertion.
// insertattail.cpp
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next, *prev;
Node(int new_data) {
data = new_data;
next = prev = nullptr;
}
};
Node *insertEnd(Node *head, int new_data) {
Node *newNode = new Node(new_data);
if (head == NULL) {
head = newNode;
}
else {
Node *curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = newNode;
newNode->prev = curr;
}
return head;
}
void display(Node *head) {
Node *curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
Node *head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
cout << "Original Linked List: ";
display(head);
cout << "Inserting 4 at the end: ";
int data = 4;
head = insertEnd(head, data);
display(head);
return 0;
}print("We're Working")console.log("We're Working");How is this guide?