Algorithm for Insertion in a Doubly Linked List
Algorithm for Insertion in a Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
if (*head != NULL) {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
if (*head == NULL) {
*head = newNode;
return;
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
if (position <= 0) {
printf("Invalid position\n");
return;
if (position == 1) {
insertAtBeginning(head, data);
return;
temp = temp->next;
if (temp == NULL) {
return;
newNode->next = temp->next;
if (temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
temp = temp->next;
printf("NULL\n");
// Driver code
int main() {
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
printList(head);
insertAtBeginning(&head, 5);
printList(head);
return 0;
1. Node Structure:
2. Insertion Functions:
4. Main Function:
This code efficiently manages a doubly linked list and allows insertion at different positions