# Find length of Doubly Linked List

This is basic problem how to count length of doubly linked list. We can solve this problem using by linked list traversal.

Process: Use one counter integer variable, and use one Linked list pointer which that is point to front node of linked list. and iterate one by one nodes of this linked list and increment counter value one by one. When this pointer are NULL then stop execution process and display counter value. Time complexity of this process are O(n). n is number of linked list nodes.

Suppose we are inserted the following (4, 3, 2, 5, 3) node in a sequence.

Here given code implementation process.

``````//C Program
//Find the length of doubly linked list
#include <stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node
{
int data;
struct Node*next;
struct Node*prev;
};

//insert Node element of end of linked list
{

//Create a dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL)
{
printf("Memory overflow\n");
}
else
{
//set data value
node->data=value;
node->next=NULL;
node->prev=NULL;
{
}
else
{
//find last node
while(temp!=NULL && temp->next!=NULL)
{
temp=temp->next;
}
//add new node to last positions
temp->next=node;
node->prev=temp;

}
}
}
//display element of Node
void display(struct Node*temp)
{

if(temp==NULL)
{
}
else
{

//Traverse doubly linked list from front to rear
while(temp!=NULL)
{
//print node value
printf("%d  ",temp->data);
temp=temp->next;
}
}

}
//count length of doubly linked list
void  length_count(struct Node*temp)
{
if(temp!=NULL)
{

int counter=0;
//get remove node
while(temp!=NULL)
{
temp=temp->next;
counter++;
}
printf("\nLength  %d\n",counter);

}else
{

}
}

int main()
{
//set node pointer value

//display all node

return 0;
}``````

#### Output

``````Linked List Elements :4  3  2  5  3
Length  5
``````
``````//C++ Program
//Find the length of doubly linked list
#include <iostream>
using namespace std;
//create structure
struct Node
{
int data;
Node*next;
Node*prev;
};
class DoublyList
{
public:
DoublyList();
void insert(int);
void display();
void length_count();
};
//set inital value
DoublyList::DoublyList()
{
}

//insert a linked list Node element
void DoublyList:: insert(int value)
{
//Create a dynamic node
Node*node=new Node;
if(node==NULL)
{
cout<<"Memory overflow\n";
}
else
{
//set data value
node->data=value;
node->next=NULL;
node->prev=NULL;
{
}else
{
//find last node
while(temp!=NULL && temp->next!=NULL)
{
temp=temp->next;
}
//add new node to last positions
temp->next=node;
node->prev=temp;

}
}
}
//display all element of doubly linked list
void DoublyList::display()
{

{
}
else
{
while(temp!=NULL)
{
//display node elemement
cout<<" "<<temp->data;
temp=temp->next;
}
}

}
//count length of doubly linked list
void  DoublyList:: length_count()
{
{
}
else
{
int counter=0;
//get remove node
while(temp!=NULL)
{
temp=temp->next;
counter++;
}
cout<<"\nLength "<<counter<<endl;

}
}

int main()
{
DoublyList obj=DoublyList();

obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(5);
obj.insert(3);
obj.display();
obj.length_count();
return 0;
}``````

#### Output

``````Linked List Element : 4 3 2 5 3
Length 5
``````
``````//Java program
//Find the length of doubly linked list
class Node
{
public int data;
public Node next,prev;
}
{

//Class constructors
{
}
public void insert(int value)
{
//Create a dynamic node
Node node=new Node();
//add node value and pointer value
node.data=value;
node.next=null;
node.prev=null;
//when no element
else
{
//find last node
while(temp.next!=null) temp=temp.next;
node.prev=temp;
temp.next=node;
}
}
//display all Linked List node value
public void display()
{
{
while(temp!=null)
{
//display node value
System.out.print("  "+temp.data);
temp=temp.next;
}
}
else
{
}
}
//count length of doubly linked list
public void  lengthCount()
{
{
}
else
{
int counter=0;
//get remove node
while(temp!=null)
{
temp=temp.next;
counter++;
}
System.out.println("\n Length "+counter);

}
}

public static void main(String[] args)
{

obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(5);
obj.insert(3);

//display all node
obj.display();

obj.lengthCount();

}
}``````

#### Output

`````` Linked List Element :  4  3  2  5  3
Length 5
``````
``````#Python Program
#Find the length of doubly linked list
class Node:
def __init__(self,data):
self.data=data
self.next=None
self.prev=None

def __init__(self):

#insert new node to end of Linked List
def insert(self,data):
#make a new node
node=Node(data)

#when empty list
else:
#find last node
while temp.next!=None:
temp=temp.next
temp.next=node
node.prev=temp
#find length
def lengthCount(self):
else:
counter=0
while temp!=None:
temp=temp.next
counter+=1
print("Length : ",counter)

#view all node values
def display(self):
return

while(temp!=None):
#print node value
print(temp.data,end=" ")
temp=temp.next
print("\n")
def main():

obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(5);
obj.insert(3);
#display all node
obj.display();

obj.lengthCount();

if(__name__=="__main__"):
main()``````

#### Output

``````Linked List Elements :
4 3 2 5 3

Length :  5
``````
``````//C# program
//Find the.Length of doubly linked list
using System;
public class Node
{
public int data;
public Node next,prev;
}
{

//Class constructors
{
}
public void insert(int value)
{
//Create a dynamic node
Node node=new Node();
//add node value and pointer value
node.data=value;
node.next=null;
node.prev=null;
//when no element
else
{
//find last node
while(temp.next!=null) temp=temp.next;
node.prev=temp;
temp.next=node;
}
}
//display all Linked List node value
public void display()
{
{
while(temp!=null)
{
//display node value
Console.Write("  "+temp.data);
temp=temp.next;
}
}
else
{
}
}
public void lengthCount()
{
{
}
else
{
int counter=0;
//get remove node
while(temp!=null)
{
temp=temp.next;
counter++;
}
Console.WriteLine("\n.Length "+counter);

}
}

public static void Main(String[] args)
{

obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(5);
obj.insert(3);

//display all node
obj.display();

obj.lengthCount();

}
}``````

#### Output

``````Linked List :  4  3  2  5  3
Length 5``````
``````<?php
//Php program
//Find the length of doubly linked list
class Node
{
public \$data;
public \$next;
public \$perv;
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
\$this->prev = NULL;
}
}
{

function __construct()
{
}
/*
* Append the Given data value at Beginning of linked list
* Fun : insert
* Parm: data value
*@return None
*/
function insert(\$data)
{
//Create a dynamic node
\$node=new Node(\$data);
if(\$node==NULL)
{
echo "Memory overflow\n";
}
else
{
{
}
else
{
//find last node
while(\$temp!=NULL && \$temp->next!=NULL)
{
\$temp=\$temp->next;
}
//add new node to last positions
\$temp->next=\$node;
\$node->prev=\$temp;

}
}
}
//Display all inserted node in linked list
function display()
{
{
}
else
{
while(\$temp!=NULL)
{
//display node value
echo "  ".\$temp->data;
\$temp=\$temp->next; //visit to next node
}
}
}
//count length of doubly linked list
public function  length_count()
{
{
}
else
{
\$counter=0;
//get remove node
while(\$temp!=NULL)
{
\$temp=\$temp->next;
\$counter++;
}
echo "\nLength ".\$counter."\n";

}
}
}
function main()
{
//Make a object of LinkedList class

\$obj->insert(4);
\$obj->insert(3);
\$obj->insert(2);
\$obj->insert(5);
\$obj->insert(3);
\$obj->display();
\$obj->length_count();
}
main();
?>```
```

#### Output

``````Doubly Linked List :  4  3  2  5  3
Length 5
``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.