﻿ C++ - Middle element of a Doubly Linked List

# C++ Linked List Exercises: Middle element of a Doubly Linked List

## C++ Linked List: Exercise-20 with Solution

Write a C++ program to find the middle element of a given Doubly Linked List.

Test Data:
Doubly linked list is as follows:
------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
The middle element is: Green
Insert a new node at the end of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction: Pink Orange White Green Red
Traversal in Reverse direction: Red Green White Orange Pink
The middle element is: White

Sample Solution:

C++ Code:

``````#include <iostream> // Including input-output stream header file

using namespace std; // Using standard namespace

// A doubly linked list node
struct Node {
string data; // Data field to store string data
struct Node* next; // Pointer to the next node
struct Node* prev; // Pointer to the previous node
};

// Function to append data at the front of the doubly linked list
{
// Create a new node and allocate memory.
struct Node* newNode = new Node;

// Assign data to the new node
newNode->data = new_data;

// A new node has been added with the name head and the previous node
// set to null, since it is being added at the front.
newNode->prev = NULL;

// Previous head is the new node

// Head points to the new node
}

// Function to find and display the middle element of the doubly linked list
{

{
while (fastptr != NULL && fastptr->next != NULL)
{
fastptr = fastptr->next->next;
slowptr = slowptr->next;
}
cout<<"\nThe middle element is: "<<slowptr->data;
}
}

// Following function displays contents of the doubly linked list
{
Node* last_node;
cout << "\nTraversal in Forward direction: ";
cout << " " << head->data << " "; // Displaying data in forward direction
}
cout << "\nTraversal in Reverse direction: ";
while (last_node != NULL) {
cout << " " << last_node->data << " "; // Displaying data in reverse direction
last_node = last_node->prev;
}
}

// Main program
int main() {
append_data(&head, "Red"); // Appending "Red" at the front of the list
append_data(&head, "Green"); // Appending "Green" at the front of the list
append_data(&head, "White"); // Appending "White" at the front of the list
append_data(&head, "Orange"); // Appending "Orange" at the front of the list
cout<<"Doubly linked list is as follows: ";
cout<<"\n---------------------------------";
Middle_element(&head); // Finding and displaying the middle element
cout<<"\n\nInsert a new node:";
cout<<"\n--------------------";
append_data(&head, "Pink"); // Inserting "Pink" node at the front of the list
Middle_element(&head); // Finding and displaying the new middle element
cout<<endl;
return 0; // Returning from the main function
}
``````

Sample Output:

```Doubly linked list is as follows:
---------------------------------
Traversal in Forward direction:  Orange  White  Green  Red
Traversal in Reverse direction:  Red  Green  White  Orange
The middle element is: Green

Insert a new node:
--------------------
Traversal in Forward direction:  Pink  Orange  White  Green  Red
Traversal in Reverse direction:  Red  Green  White  Orange  Pink
The middle element is: White
```

Flowchart:

CPP Code Editor: