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

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. ## 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);
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;
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;
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  