+92 332 4229 857 99ProjectIdeas@Gmail.com

How to find the largest element in a singly linked list (C++)


Finding largest in the linked list
GetLargest() returns the largest element in the linked list.
Summary of GetLargest()
First of all a new node named tptr is created and is initialized by the head of the linked lsit.
Node*tptr=head;
A variable named largest is also created to store the largest element of the linked list and is initialized by the data of the first node.
       int largest=head->data;
After that a loop is created which starts from the first node and compares the data of the current node with the current largest, if the element of the current node is larger than the value in the largest variable, the data of the current node is saved in the largest variable, and this continues till the end of linked list.
              while ( tptr != NULL )
              {
                     if ( tptr->data > largest )
                           largest = tptr->data;
                     tptr = tptr->link;
              }
And at the last the largest element is returned from the function.
       return largest;

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

struct Node
{
public:
       int data;
       Node*link;
      
       Node();
};

Node::Node()
{
       data=0;
       link=NULL;
}

class LinkList
{
public:
       Node*head;
       LinkList();
       void AddNodeAtEnd(int);
       int  GetLargest();
       void DisplayWhole();
};

LinkList::LinkList()
{
       head=NULL;
}

void LinkList::AddNodeAtEnd(int _data)
{
       Node*node=new Node();
       node->data=_data;
       node->link=NULL;
      
       if(head==NULL)
              head=node;
       else
       {
              Node*tptr=head;
              while(tptr->link!=NULL)
                     tptr=tptr->link;
              tptr->link=node;
       }
}

int LinkList::GetLargest()
{
       Node*tptr=head;
       int largest=head->data;

              while ( tptr != NULL )
              {
                     if ( tptr->data > largest )
                           largest = tptr->data;
                     tptr = tptr->link;
              }
       return largest;
}

void LinkList::DisplayWhole()
{
       Node*temp=head;

       while(temp!=NULL)
       {
              cout<<temp->data<<"  ";
              temp=temp->link;
       }
}

int main()
{
       LinkList l;

       // Adding first    element to the linked list
       l.AddNodeAtEnd(10);
       // Adding second   element to the linked list
       l.AddNodeAtEnd(20);
       // Adding third    element to the linked list
       l.AddNodeAtEnd(30);
       // Adding fourth   element to the linked list
       l.AddNodeAtEnd(40);
       // Adding fifth    element to the linked list
       l.AddNodeAtEnd(40);
       // Adding sixth    element to the linked list
       l.AddNodeAtEnd(50);
       // Adding seventh  element to the linked list
       l.AddNodeAtEnd(60);
       // Adding eighth   element to the linked list
       l.AddNodeAtEnd(70);
       // Adding ninth    element to the linked list
       l.AddNodeAtEnd(80);

       // Displaying all nodes in the linked list
       cout<<"Elements in the linked list are : "<<endl;
       l.DisplayWhole();
       cout<<endl<<endl;

       cout << "Largest  element in the linked list : " << l.GetLargest() << endl;
      
       _getche();
       return 0;
}

Output
Elements in the linked list are :
10  20  30  40  40  50  60  70  80

Largest  element in the linked list : 80

0 comments: