

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find minimum and maximum elements in singly Circular Linked List in C++
Here we will see how to get the minimum and maximum value from one singly circular linked linear list. The basic concept is very simple. The next part of the last node will be pointed to the first node, the first node will also be pointed using start pointer. When we insert some element into the list, after inserting the next part of the newly inserted node will be updated with the address of the start node.
Initially the min is assigned with positive infinity, and max is assigned with negative infinity. Now traverse the list from left to right. If the current element is less than the min element, then update min, if the current element is larger than the max element, then update the max. Thus we can get the min and max values.
Example
#include<iostream> using namespace std; class Node{ public: int data; Node *next; }; Node* getNode(int key){ Node *newNode = new Node(); newNode->data = key; newNode->next = NULL; return newNode; } void insert(Node **start, int data){ Node *current = *start; Node *newNode = getNode(data); if(*start == NULL){ newNode->next = newNode; *start = newNode; return; } while (current->next != *start) { current = current->next; } newNode->next = *start; current->next = newNode; } void displayList(Node *start){ Node* current = start; if (start == NULL) { cout << "Display List is empty"; return; } else { do { cout << current->data << " "; current = current->next; } while (current != start); } cout << endl; } void getMinMax(Node **start){ if(*start == NULL){ return; } Node* current; current = *start; int min = INT_MAX, max = INT_MIN; while (current->next != *start) { if (current->data < min) { min = current->data; } if (current->data > max) { max = current->data; } current = current->next; } cout << "Minimum: " << min << ", Maximum: " << max; } int main() { int data[] = {99, 11, 22, 10, 44, 55, 66}; int n = sizeof(data)/sizeof(data[0]); Node *start = NULL; for(int i = 0; i<n; i++){ insert(&start, data[i]); } displayList(start); getMinMax(&start); }
Output
99 11 22 10 44 55 66 Minimum: 10, Maximum: 99
- Related Questions & Answers
- Singly Linked List as Circular in Javascript
- Convert singly linked list into circular linked list in C++
- Minimum and Maximum Prime Numbers of a Singly Linked List in C++.
- Find smallest and largest elements in singly linked list in C++
- C++ Program to Implement Circular Singly Linked List
- Python program to find the maximum and minimum value node from a circular linked list
- Remove elements from singly linked list in JavaScript
- Difference between Singly linked list and Doubly linked list in Java
- Python Program to Convert a given Singly Linked List to a Circular List
- Circular Linked List in C++
- Find middle of singly linked list Recursively in C++
- Convert singly linked list into XOR linked list in C++
- Find the common nodes in two singly linked list in C++
- Python Circular Linked List Program
- Check if a linked list is Circular Linked List in C++
Advertisements