+92 332 4229 857 99ProjectIdeas@Gmail.com

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


Finding smallest in the linked list
GetSmallest() returns the smallest element in the linked list.
Summary of GetSmallest()
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 smallest is also created to store the smallest element of the linked list and is initialized by the data of the first node.
       int smallest=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 smallest, if the element of the current node is smaller than the value in the smallest variable, the data of the current node is saved in the smallest variable, and this continues till the end of linked list.
              while ( tptr != NULL )
              {
                     if ( tptr->data < smallest )
                           smallest = tptr->data;
                     tptr = tptr->link;
              }
And at the last the smallest element is returned from the function.
       return smallest;

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  GetSmallest();
       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::GetSmallest()
{
       Node*tptr=head;
       int smallest=head->data;

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

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 << "Smallest element in the linked list : " << l.GetSmallest() << endl;

       _getche();
       return 0;
}

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

Smallest element in the linked list : 10

0 comments: