0% found this document useful (0 votes)
2 views

Algorithm for Insertion in a Doubly Linked List

The document outlines an algorithm and C code for inserting nodes into a doubly linked list. It describes how to create a new node, insert at the beginning, end, or a specific position, and includes functions for creating nodes and printing the list. The main function demonstrates the usage of these insertion methods and displays the list after each operation.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm for Insertion in a Doubly Linked List

The document outlines an algorithm and C code for inserting nodes into a doubly linked list. It describes how to create a new node, insert at the beginning, end, or a specific position, and includes functions for creating nodes and printing the list. The main function demonstrates the usage of these insertion methods and displays the list after each operation.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithm for Insertion in a Doubly Linked List

1. Create a new node with the given data.

2. Check if the list is empty:

o If yes, make the new node the head.

3. Insert at the beginning:

o Update the new node’s next to point to the current head.

o Update the current head’s prev to point to the new node.

o Make the new node the new head.

4. Insert at the end:

o Traverse to the last node.

o Update the last node’s next to point to the new node.

o Set the new node’s prev to the last node.

5. Insert at a specific position:

o Traverse to the desired position.

o Adjust pointers of the adjacent nodes to include the new node.

C Code for Insertion in a Doubly Linked List

#include <stdio.h>

#include <stdlib.h>

// Define a node structure

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

// Function to create a new node

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

// Function to insert at the beginning

void insertAtBeginning(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head != NULL) {

newNode->next = *head;

(*head)->prev = newNode;

*head = newNode;

// Function to insert at the end

void insertAtEnd(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

return;

struct Node* temp = *head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;
}

// Function to insert at a specific position (1-based index)

void insertAtPosition(struct Node** head, int data, int position) {

if (position <= 0) {

printf("Invalid position\n");

return;

if (position == 1) {

insertAtBeginning(head, data);

return;

struct Node* newNode = createNode(data);

struct Node* temp = *head;

for (int i = 1; temp != NULL && i < position - 1; i++) {

temp = temp->next;

if (temp == NULL) {

printf("Position out of range\n");

return;

newNode->next = temp->next;

if (temp->next != NULL)

temp->next->prev = newNode;
temp->next = newNode;

newNode->prev = temp;

// Function to print the list

void printList(struct Node* head) {

struct Node* temp = head;

printf("Doubly Linked List: ");

while (temp != NULL) {

printf("%d <-> ", temp->data);

temp = temp->next;

printf("NULL\n");

// Driver code

int main() {

struct Node* head = NULL;

insertAtEnd(&head, 10);

insertAtEnd(&head, 20);

insertAtEnd(&head, 30);

printList(head);

insertAtBeginning(&head, 5);

printList(head);

insertAtPosition(&head, 15, 3);


printList(head);

return 0;

Explanation of the Code

1. Node Structure:

o data: Stores the integer value.

o prev: Pointer to the previous node.

o next: Pointer to the next node.

2. Insertion Functions:

o insertAtBeginning(): Inserts a node at the start.

o insertAtEnd(): Appends a node to the end.

o insertAtPosition(): Inserts a node at a specific position.

3. Printing the List:

o printList(): Prints the doubly linked list.

4. Main Function:

o Creates an empty list.

o Inserts elements in different positions.

o Displays the list after each operation.

This code efficiently manages a doubly linked list and allows insertion at different positions

You might also like