# Find the largest node in Doubly linked list

Suppose that given linked list is combination of integer value. we are need to find largest node of this doubly linked list. We can solve this problem by using linked list traversal from front node to last node.

Process: When linked list is not empty then use one integer variable and assign front node value to this integer variable. Using of linked list pointer iterates linked list nodes are one by one visiting upcoming nodes. And check that linked list element value are greater than integer variable values . Then assign this value to resultant variable. This process are execute until not found last linked list node. This process are time complexity is O(n).

Suppose we are inserted the following (14, 31, 12, 15, 11, 25 ) node in a sequence.

Here given code implementation process.

``````//C Program
//Find out max value node in 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;
}
}

}
//Find largest node element
void  big_node(struct Node*temp)
{
if(temp!=NULL)
{

int big=temp->data;
temp=temp->next;
//get remove node
while(temp!=NULL)
{
if(temp->data>big)
{
big=temp->data;
}
temp=temp->next;
}
printf("\nBig Element : %d\n",big);

}
else
{

}
}

int main()
{
//set node pointer value

//display all node

return 0;
}``````

#### Output

``````Linked List Elements :14  31  12  15  11  25
Big Element : 31
``````
``````//C++ Program
//find maximum element in 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 big_node();
};
//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;
}
}

}

//Find largest node element
void DoublyList:: big_node()
{
{
int big=temp->data;
temp=temp->next;
//get remove node
while(temp!=NULL)
{
if(temp->data>big)
{
big=temp->data;
}
temp=temp->next;
}
cout<<"\nBig Element : "<<big<<endl;

}
else
{

}
}

int main()
{
DoublyList obj=DoublyList();
obj.insert(14);
obj.insert(31);
obj.insert(12);
obj.insert(15);
obj.insert(11);
obj.insert(25);
obj.display();
obj.big_node();
return 0;
}``````

#### Output

``````Linked List Element : 14 31 12 15 11 25
Big Element : 31
``````
``````//Java program
//Find largest node in 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
{
}
}
//Find largest node element
public void  bigNode()
{
{
//get remove node
while(temp!=null)
{
if(temp.data>big)
{
big=temp.data;
}
temp=temp.next;
}
System.out.println("\nBig Element :"+big);

}
else
{

}
}

public static void main(String[] args)
{

obj.insert(14);
obj.insert(31);
obj.insert(12);
obj.insert(15);
obj.insert(11);
obj.insert(25);

//display all node
obj.display();

obj.bigNode();

}
}``````

#### Output

``````Linked List Element : 14 31 12 15 11 25
Big Element : 31
``````
``````#Python Program
#Find largest node in 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 out maximum element in doubly linked list
def bigNode(self):
else:
while temp!=None:
if(big<temp.data):
#new big element
big=temp.data
temp=temp.next

print("Big node : ",big)

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

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

obj.insert(14);
obj.insert(31);
obj.insert(12);
obj.insert(15);
obj.insert(11);
obj.insert(25);
#display all node
obj.display();

obj.bigNode();

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

#### Output

``````Linked List Elements :
14 31 12 15 11 25

Big node :  31
``````
``````//C# program
//Find largest node in 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
{
}
}
//Find largest node element
public void  bigNode()
{
{
//get remove node
while(temp!=null)
{
if(temp.data>big)
{
big=temp.data;
}
temp=temp.next;
}
Console.WriteLine("\nBig Element :"+big);

}
else
{

}
}

public static void Main(String[] args)
{

obj.insert(14);
obj.insert(31);
obj.insert(12);
obj.insert(15);
obj.insert(11);
obj.insert(25);

//display all node
obj.display();

obj.bigNode();

}
}``````

#### Output

``````Linked List :  14  31  12  15  11  25
Big Element : 31``````
``````<?php
//Php program
//Find maximum element in 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
}
}
}
//find largest node
public function  big_node()
{
{
}
else
{

while(\$temp!=NULL)
{
if(\$big<\$temp->data)
{
\$big=\$temp->data;
}
\$temp=\$temp->next;

}
echo "\nBig node is ".\$big."\n";

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

\$obj->insert(14);
\$obj->insert(31);
\$obj->insert(12);
\$obj->insert(15);
\$obj->insert(11);
\$obj->insert(25);
\$obj->display();
\$obj->big_node();
}
main();
?>``````

#### Output

``````Doubly Linked List :  14  31  12  15  11  25
Big node is 31``````

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.