w3resource

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>

using namespace std;
  
// A doubly linked list node
struct Node {
   string data;
   struct Node* next;
   struct Node* prev;
   Node *link;
};

  
//Append node at the front of the list
void append_data(Node** head, string new_data)
{
   //Create a new node and allocate memory.
   struct Node* newNode = new Node;
  
   //assign data to 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->next = (*head);
   newNode->prev = NULL;
  
   //previous head is new node
   if ((*head) != NULL)
   (*head)->prev = newNode;
  
   //head points to new node
   (*head) = newNode;
}

      
void Middle_element(Node** head)  
{  
    struct Node *slowptr = *head;  
    struct Node *fastptr = *head;  
  
    if (head!=NULL)  
    {  
        while (fastptr != NULL && fastptr->next != NULL)  
        {  
            fastptr = fastptr->next->next;  
            slowptr = slowptr->next;  
        }  
        cout<<"\nThe middle element is: "<<slowptr->data;  
    }  
}
 
// Following function display contents of the doubly linked list
void displayDlList(Node* head)
{
    Node* last_node;
    cout << "\nTraversal in Forward direction: ";
    while (head != NULL) {
        cout << " " << head->data << " ";
        last_node = head;
        head = head->next;
    }
    cout << "\nTraversal in Reverse direction: ";
    while (last_node != NULL) {
        cout << " " << last_node->data << " ";
        last_node = last_node->prev;
    }
}

//main program
int main() {
   /* Start with the empty list */
   struct Node* head = NULL;
   append_data(&head, "Red");
   append_data(&head, "Green");
   append_data(&head, "White");
   append_data(&head, "Orange");
   cout<<"Doubly linked list is as follows: ";  
   cout<<"\n---------------------------------";
   displayDlList(head);
   Middle_element(&head);
   cout<<"\n\nInsert a new node:";
   cout<<"\n--------------------";
   append_data(&head, "Pink");
   displayDlList(head);
   Middle_element(&head);
   cout<<endl;
   return 0;
}

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:

Flowchart: Middle element of a Doubly Linked List.
Flowchart: Middle element of a Doubly Linked List.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Insert a new node at the end of a Doubly Linked List.
Next C++ Exercise: Insert new node at the middle of a Doubly Linked List.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.