Find minimum and maximum elements in singly Circular Linked List | C ++ program to find minimum and maximum value from singly circular linked list ; structure for a node ; Function to print minimum and maximum nodes of the circular linked list ; check list is empty ; pointer for traversing ; initialize head to current pointer ; initialize max int value to min initialize min int value to max ; While last node is not reached ; If current node data is lesser for min then replace it ; If current node data is greater for max then replace it ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move firt node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Function to print the Circular linked list ; if list is empty simply show message ; traverse first to last node ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void printMinMax ( struct Node * * head ) { if ( * head == NULL ) { return ; } struct Node * current ; current = * head ; int min = INT_MAX , max = INT_MIN ; do { if ( current -> data < min ) { min = current -> data ; } if ( current -> data > max ) { max = current -> data ; } current = current -> next ; } while ( current != head ) ; cout << " Minimum = " ▁ < < ▁ min ▁ < < ▁ " , Maximum = " } void insertNode ( struct Node * * head , int data ) { struct Node * current = * head ; struct Node * newNode = new Node ; if ( ! newNode ) { printf ( " Memory Error " return ; } newNode -> data = data ; if ( * head == NULL ) { newNode -> next = newNode ; * head = newNode ; return ; } else { while ( current -> next != * head ) { current = current -> next ; } newNode -> next = * head ; current -> next = newNode ; } } void displayList ( struct Node * head ) { struct Node * current = head ; if ( head == NULL ) { printf ( " Display List is empty " return ; } else { do { printf ( " % d ▁ " , current -> data ) ; current = current -> next ; } while ( current != head ) ; } } int main ( ) { struct Node * Head = NULL ; insertNode ( & Head , 99 ) ; insertNode ( & Head , 11 ) ; insertNode ( & Head , 22 ) ; insertNode ( & Head , 33 ) ; insertNode ( & Head , 44 ) ; insertNode ( & Head , 55 ) ; insertNode ( & Head , 66 ) ; cout << " Initial ▁ List : ▁ " ; displayList ( Head ) ; printMinMax ( & Head ) ; return 0 ; }