C++ program to find middle of the linked list

C++ program to find middle of the linked list

Finding the middle  element of a Singly Linked List in C++?

Finding middle of anything means to find the center position it. In this article, we will be learning how to code a C++ program to find middle of the linked list. We will take two pointers for the process one pointer will be moving with twice the speed of second pointer. So when the first pointer will reach the end of the liked list, the second pointer will be at middle of it as it is moving with half the speed of first pointer. In this way center node of the linked list is fetched.

 

How to define a linked list?

class Node  

    int data; 
    Node *next; 
}; 

Steps to write a C++ program for finding  middle of a linked list

These steps are used to find middle the linked list in CPP programming

  1. Start the program and initialize the variables.
  2. Create a function to create and display list.
  3. Now create a function to find middle of the list.
  4. In this function initialize two pointers.
  5. Traverse one pointer with normal speed.
  6. Traverse other pointer with double speed.
  7. When the pointer moving with double speed reaches end of the list other pointer will be at middle.
  8. Print data at the address of this pointer.
c++ program to find the middle of the linked list

Algorithm for finding the middle of a linked list

Algorithm to find middle of the linked list in CPP programming is given below
  • SINGLE_PTR=STNODE
  • TWICE_PTR=STNODE
  • IF(STNODE!=NULL)
  • WHILE(TWICE_PTR!=NULL&&TWICE_PTR->NEXTPTR!=NULL
  • TWICE_PTR=TWICE_PTR->NEXTPTR->NEXTPTR
  • SINGLE_PTR=SINGLE_PTR->NEXTPTR
  • PRINT SINGLE_PTR->NUM

C++ Program to find middle of linked list

Run
#include < iostream>
using namespace std;

struct node {
    int num;
    node* nextptr;
} *stnode; // node constructed

void make(int n);
void printMiddle(struct node* stnode);
void display();

int main() // Main method
{
    int n;

    cout << "Enter the number of nodes: ";
    cin >> n;
    make(n);
    cout << "\nLinked list data: \n";
    display();
    printMiddle(stnode);

    return 0;
}

void make(int n) // Function to create a linked list
{
    struct node* frntNode, *tmp;
    int num, i;

    stnode = (struct node*)malloc(sizeof(struct node));
    if (stnode == NULL)
    {
        cout << "Memory cannot be allocated";
    }
    else
    {
        cout << "Enter the data for node 1: ";
        cin >> num;
        stnode->num = num;
        stnode->nextptr = NULL; // Links the address field to NULL
        tmp = stnode;

        for (i = 2; i <= n; i++)
        {
            frntNode = (struct node*)malloc(sizeof(struct node));

            if (frntNode == NULL)
            {
                cout << "Memory cannot be allocated";
                break;
            }
            else
            {
                cout << "Enter the data for node " << i << ": ";
                cin >> num;
                frntNode->num = num;
                frntNode->nextptr = NULL;
                tmp->nextptr = frntNode;
                tmp = tmp->nextptr;
            }
        }
    }
}

void display() // Function to display the linked list
{
    struct node* tmp;
    if (stnode == NULL)
    {
        cout << "List is empty";
    }
    else
    {
        tmp = stnode;
        cout << "Linked List: ";
        while (tmp != NULL)
        {
            cout << tmp->num << " \t ";
            tmp = tmp->nextptr;
        }
    }
}

void printMiddle(struct node* stnode) // Function to print the middle node of the list
{
    struct node* single_ptr = stnode;
    struct node* twice_ptr = stnode;

    if (stnode != NULL)
    {
        while (twice_ptr != NULL && twice_ptr->nextptr != NULL)
        {
            twice_ptr = twice_ptr->nextptr->nextptr; // Moving with twice speed.
            single_ptr = single_ptr->nextptr;
        }
        cout << "The middle element is " << single_ptr->num << endl;
    }
}
Output:
Enter the number of nodes: 5
Enter the data for node 1: 5
Enter the data for node 2: 9
Enter the data for node 3: 8
Enter the data for node 4: 4
Enter the data for node 5: 6

Linked list data: 
Linked List	5	9	8	4	6	
The middle element is 8

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java