C++ program to find middle of the linked list

Program to find middle of the linked list in C++

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; 
}; 

Following set of code is used to define a linked list in our program.

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 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

#include <iostream>
using namespace std;

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

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

 
int main() //Main method
{
    int n,num;
		
    cout<<"Enter the number of nodes: ";
    cin>>n;
    make(n);
    cout<<"\nLinked list data: \n";		
    print();
    printMiddle(stnode);
   
    return 0;
}
void make(int n) //function to create linked list.
{
    struct node *frntNode, *tmp;
    int num, i;
 
    stnode = (struct node *)malloc(sizeof(struct node));
    if(stnode == NULL)        
    {
        cout<<"Memory can not 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) "//If frntnode is null no memory cannot be allotted
            {
                cout<<"Memory can not be allocated";
                break;
            }
            else
            {
                cout<<"Enter the data for node "<<i<<": "; // Entering data in nodes.
                cin>>num;
                frntNode->num = num;         
                frntNode->nextptr = NULL;    
                tmp->nextptr = frntNode;     
                tmp = tmp->nextptr;
            }
        }
    }
} 
void print() // method to display linked list
{
    struct node *tmp;
    if(stnode == NULL)
    {
        cout<<"List is empty";
    }
    else
    {
        tmp = stnode;
        cout<<"Linked List\t";
        while(tmp != NULL)
        {
            cout<<tmp->num<<"\t";   
            tmp = tmp->nextptr;                
        }
    }
}
void printMiddle(struct node *stnode)//function to print 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;
    }
}
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
Quiz time

Fun Fact

Have you ever wonder how the process has changed from printing middle of array to printing middle of linked. Printing middle of array is a very simple task find the length, divide it by two and you are done. No need to do this complicated pointer thing. Well that the difference between them just like our superheroes  IRON MAN and SPIDERMAN .

Deletion in a linked list