Thursday, 29 October 2026

To display the contents in reverse order of doubly linked list.

 #include <iostream>

using namespace std;


struct Node

{

    int data;

    Node *prev;

    Node *next;

};

int main()

{

    Node *head, *temp;


    // Creating nodes

    Node *n1 = new Node{10, NULL, NULL};

    Node *n2 = new Node{20, NULL, NULL};

    Node *n3 = new Node{30, NULL, NULL};

    Node *n4 = new Node{40, NULL, NULL};


    head = n1;


    n1->next = n2;

    n2->prev = n1;


    n2->next = n3;

    n3->prev = n2;


    n3->next = n4;

    n4->prev = n3;


    // Move to last node

    temp = head;


    while (temp->next != NULL)

    {

        temp = temp->next;

    }


    // Display in reverse order

    cout << "Reverse List: ";


    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->prev;

    }


    return 0;

}

To delete a node from the end of a doubly linked list.

 #include <iostream>

using namespace std;


struct Node

{

    int data;

    Node *prev;

    Node *next;

};


int main()

{

    Node *head, *temp;


    // Creating nodes

    Node *n1 = new Node{10, NULL, NULL};

data – Stores the value.

prev – Stores the address of the previous node.

next – Stores the address of the next node.


    Node *n2 = new Node{20, NULL, NULL};

    Node *n3 = new Node{30, NULL, NULL};


    head = n1;


    n1->next = n2;

    n2->prev = n1;


    n2->next = n3;

    n3->prev = n2;


    // Display original list

    cout << "Before Deletion: ";

    temp = head;


    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->next;

    }


    // Delete last node

    temp = head;


    while (temp->next != NULL)

    {

        temp = temp->next;

    }


    temp->prev->next = NULL;


    delete temp;


    // Display updated list

    cout << "\nAfter Deletion: ";

    temp = head;


    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->next;

    }


    return 0;

}


.Insert a node at the end of a doubly linked list

 #include <iostream>

using namespace std;

struct Node

{

    int data;

    Node* prev;

    Node* next;

};


int main()

{

    Node* head = NULL;

    Node* temp;

    Node* newNode;


    // Create first node

    head = new Node;

    head->data = 10;

    head->prev = NULL;

    head->next = NULL;


    // Create second node

    temp = new Node;

    temp->data = 20;

    temp->prev = head;

    temp->next = NULL;

    head->next = temp;




  // Insert new node at the end

    newNode = new Node;

    newNode->data = 30;

    newNode->next = NULL;

    newNode->prev = temp;

    temp->next = newNode;


    // Display the list

    temp = head;


    cout << "Doubly Linked List: ";


    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->next;

    }


    return 0;

}

head->next = temp; connects node 10 to node 20.

temp->next = newNode; connects node 20 to node 30.

newNode->prev = temp; connects node 30 backward to node 20.


  • First, create nodes containing 10 and 20.

  • Create a new node containing 30.

  • temp->next = newNode connects node 20 to node 30.

  • newNode->prev = temp connects node 30 back to node 20.

  • Finally, display all nodes using the next pointer.

Remember: prev points backward, and next points forward.




Tuesday, 27 October 2026

Insertion at a Specific Position


Insert 15 at position 2 in the list 10 → 20 → 30.

The position is counted starting from 1. 

#include <iostream>

using namespace std;


struct Node

{

    int data;

    Node* next;

};


int main()

{

    Node* first = new Node;

    first->data = 10;


    Node* second = new Node;

    second->data = 20;


    Node* third = new Node;

    third->data = 30;


    first->next = second;

    second->next = third;

    third->next = NULL;


    Node* newNode = new Node;

    newNode->data = 15;


    Node* temp = first;


    temp = temp->next;


    newNode->next = temp->next;

    temp->next = newNode;


    temp = first;


    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->next;

    }


    return 0;

}


Insertion at the End


Logic: Reach the last node and connect it to the new node.


#include <iostream>

using namespace std;


struct Node

{

    int data;

    Node* next;

};


int main()

{

    Node* first = new Node;

    first->data = 10;

    first->next = NULL;

  • new Node creates a node in dynamic memory.

  • first->data = 10; stores 10 in the node.

  • first->next = NULL; indicates that there is no next node yet.

Linked list:

first → [10 | NULL]


    Node* newNode = new Node;

    newNode->data = 20;

    newNode->next = NULL;

  • new Node creates another node.

  • newNode->data = 20; stores 20 in the new node.

  • newNode->next = NULL; indicates that it is currently the last node.

Now, two separate nodes exist:

[10 | NULL] [20 | NULL]

They are not connected yet.


    Node* temp = first;

temp points to the first node containing 10.

temp → [10 | NULL]


    while (temp->next != NULL)

    {

        temp = temp->next;

    }

temp->next != NULL checks whether the current node has a next node.

temp = temp->next; moves temp to the next node.


    temp->next = newNode;

This statement connects the first node to the new node.

Before:

10 → NULL 20 → NULL

After:

10 → 20 → NULL

Now the insertion at the end is complete.


    temp = first;

We set temp back to the first node so that we can display the complete linked list. 

    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->next;

    }

This loop displays each node's data and moves to the next node.

Step

temp points to

Printed value

1

Node containing 10

10

2

Node containing 20

20

3

NULL

Loop stops

  return 0;

}