Find the second largest element in a linked list in c++
C++ program for Find the second largest element in a linked list. Here problem description and other solutions.
// Include header file
#include <iostream>
using namespace std;
// C++ Program for
// Find second largest number in linked list
// Linked list node
class LinkNode
{
public: int data;
LinkNode *next;
LinkNode(int data)
{
this->data = data;
this->next = nullptr;
}
};
class SingleLL
{
public: LinkNode *head;
LinkNode *tail;
SingleLL()
{
this->head = nullptr;
this->tail = nullptr;
}
// Add new node at the end of linked list
void insert(int value)
{
// Create a new node
LinkNode *node = new LinkNode(value);
if (this->head == nullptr)
{
this->head = node;
}
else
{
this->tail->next = node;
}
this->tail = node;
}
// Display linked list element
void display()
{
if (this->head == nullptr)
{
return;
}
LinkNode *temp = this->head;
// iterating linked list elements
while (temp != nullptr)
{
cout << temp->data << " → ";
// Visit to next node
temp = temp->next;
}
cout << "null" << endl;
}
// Find second largest node in linked list
void secondLargest()
{
if (this->head == nullptr)
{
cout << "\nEmpty linked list" << endl;
return;
}
// Display given linked list element
this->display();
// Define some auxiliary variables
LinkNode *big = nullptr;
LinkNode *result = nullptr;
LinkNode *temp = this->head;
// iterating linked list elements
while (temp != nullptr)
{
if (big == nullptr)
{
// Get first node
big = temp;
}
else if (big->data < temp->data)
{
// Get a new big node
// Get second largest node
result = big;
// Get current largest node
big = temp;
}
else if (big->data != temp->data)
{
if (result == nullptr)
{
// If in case second largest node empty
result = temp;
}
else if (result->data < temp->data)
{
// Get new second largest node
result = temp;
}
}
// Visit to next node
temp = temp->next;
}
if (result == nullptr)
{
// When second largest node are not exist in linked list
cout << "Second largest element are not exist" << endl;
}
else
{
cout << "Second largest : " << result->data << endl;
}
}
};
int main()
{
// Testing linked list
SingleLL *list1 = new SingleLL();
SingleLL *list2 = new SingleLL();
SingleLL *list3 = new SingleLL();
SingleLL *list4 = new SingleLL();
// Add node in first linked list
// 6 → 4 → 5 → 10 → 3 → 7 → 9 → 2 → 8 → null
list1->insert(6);
list1->insert(4);
list1->insert(5);
list1->insert(10);
list1->insert(3);
list1->insert(7);
list1->insert(9);
list1->insert(2);
list1->insert(8);
list1->secondLargest();
// Add node in second linked list
// 1 → 1 → 4 → 2 → 3 → 1 → null
list2->insert(1);
list2->insert(1);
list2->insert(4);
list2->insert(2);
list2->insert(3);
list2->insert(1);
list2->secondLargest();
// Add node in third linked list
// 2 → 6 → 8 → 4 → 8 → 3 → 3 → 2 → null
list3->insert(2);
list3->insert(6);
list3->insert(8);
list3->insert(4);
list3->insert(8);
list3->insert(3);
list3->insert(3);
list3->insert(2);
list3->secondLargest();
// Add node in fourth linked list
list4->insert(2);
list4->insert(2);
list4->insert(2);
// 2 → 2 → 2 → null
list4->secondLargest();
return 0;
}
Output
6 → 4 → 5 → 10 → 3 → 7 → 9 → 2 → 8 → null
Second largest : 9
1 → 1 → 4 → 2 → 3 → 1 → null
Second largest : 3
2 → 6 → 8 → 4 → 8 → 3 → 3 → 2 → null
Second largest : 6
2 → 2 → 2 → null
Second largest element are not exist
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