Find largest node value of doubly linked list in node js
Js program for Find largest node value of doubly linked list. Here more information.
// Node JS program for
// Find max value in doubly linked list
// Define class of linked list Node
class LinkNode
{
constructor(data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList
{
constructor()
{
this.head = null;
this.tail = null;
}
// Insert new node at end position
insert(value)
{
// Create a new node
var node = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = node;
this.tail = node;
return;
}
// Add node at last position
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
maximum()
{
if (this.head == null)
{
// When empty linked list
return 0;
}
var result = this.head.data;
// Get first node of linked list
var temp = this.head;
// iterate linked list
while (temp != null)
{
if (temp.data > result)
{
result = temp.data;
}
// Visit to next node
temp = temp.next;
}
// Return maximum value
return result;
}
}
function main()
{
var dll = new DoublyLinkedList();
// Insert following linked list nodes
dll.insert(14);
dll.insert(31);
dll.insert(12);
dll.insert(15);
dll.insert(11);
dll.insert(25);
console.log("Maximum : " + dll.maximum());
}
// Start program execution
main();
Output
Maximum : 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.
New Comment