C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to find the maximum and minimum value node from a circular linked list
ExplanationIn this program, we will create a circular linked list then, iterate through the list to find out the minimum and maximum node. 9->5->2->7->3 We will maintain two variables min and max. Min will hold the minimum value node, and max will hold the maximum value node. In above example, 2 will be the minimum value node and 9 will be the maximum value node. Algorithm
Solution
Python
#Represents the node of list.
class Node:
def __init__(self,data):
self.data = data;
self.next = None;
class CreateList:
#Declaring head and tail pointer as null.
def __init__(self):
self.head = Node(None);
self.tail = Node(None);
self.head.next = self.tail;
self.tail.next = self.head;
#This function will add the new node at the end of the list.
def add(self,data):
newNode = Node(data);
#Checks if the list is empty.
if self.head.data is None:
#If list is empty, both head and tail would point to new node.
self.head = newNode;
self.tail = newNode;
newNode.next = self.head;
else:
#tail will point to new node.
self.tail.next = newNode;
#New node will become new tail.
self.tail = newNode;
#Since, it is circular linked list tail will point to head.
self.tail.next = self.head;
#Finds out the minimum value node in the list
def minNode(self):
current = self.head;
#Initializing min to initial node data
minimum = self.head.data;
if(self.head == None):
print("List is empty");
else:
while(True):
#If current node's data is smaller than min
#Then replace value of min with current node's data
if(minimum > current.data):
minimum = current.data;
current= current.next;
if(current == self.head):
break;
print("Minimum value node in the list: "+ str(minimum));
#Finds out the maximum value node in the list
def maxNode(self):
current = self.head;
#Initializing max to initial node data
maximum = self.head.data;
if(self.head == None):
print("List is empty");
else:
while(True):
#If current node's data is greater than max
#Then replace value of max with current node's data
if(maximum < current.data):
maximum = current.data;
current= current.next;
if(current == self.head):
break;
print("Maximum value node in the list: "+ str(maximum));
class CircularLinkedList:
cl = CreateList();
#Adds data to the list
cl.add(5);
cl.add(20);
cl.add(10);
cl.add(1);
#Prints the minimum value node in the list
cl.minNode();
#Prints the maximum value node in the list
cl.maxNode();
Output: Minimum value node in the list: 1 Maximum value node in the list: 20 C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Represents the node of list.
struct node{
int data;
struct node *next;
};
//Declaring head and tail pointer as null.
struct node *head = NULL;
struct node *tail = NULL;
//This function will add the new node at the end of the list.
void add(int data){
//Create new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
//Checks if the list is empty.
if(head == NULL){
//If list is empty, both head and tail would point to new node.
head = newNode;
tail = newNode;
newNode->next = head;
}else {
//tail will point to new node.
tail->next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will point to head.
tail->next = head;
}
}
//Finds out the minimum value node in the list
void minNode() {
struct node *current = head;
//Initializing min to initial node data
int min = head->data;
if(head == NULL) {
printf("\nList is empty");
}
else {
do{
//If current node's data is smaller than min
//Then replace value of min with current node's data
if(min > current->data) {
min = current->data;
}
current= current->next;
}while(current != head);
printf("Minimum value node in the list: %d", min);
}
}
//Finds out the maximum value node in the list
void maxNode() {
struct node *current = head;
//Initializing max to initial node data
int max = head->data;
if(head == NULL) {
printf("\nList is empty");
}
else {
do{
//If current node's data is greater than max
//Then replace value of max with current node's data
if(max < current->data) {
max = current->data;
}
current= current->next;
}while(current != head);
printf("\nMaximum value node in the list: %d", max);
}
}
int main()
{
//Adds data to the list
add(5);
add(20);
add(10);
add(1);
//Prints the minimum value node in the list
minNode();
//Prints the maximum value node in the list
maxNode();
return 0;
}
Output: Minimum value node in the list: 1 Maximum value node in the list: 20 JAVA
public class MinMax {
//Represents the node of list.
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
//Declaring head and tail pointer as null.
public Node head = null;
public Node tail = null;
//This function will add the new node at the end of the list.
public void add(int data){
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
if(head == null) {
//If list is empty, both head and tail would point to new node.
head = newNode;
tail = newNode;
newNode.next = head;
}
else {
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will points to head.
tail.next = head;
}
}
//Finds out the minimum value node in the list
public void minNode() {
Node current = head;
//Initializing min to initial node data
int min = head.data;
if(head == null) {
System.out.println("List is empty");
}
else {
do{
//If current node's data is smaller than min
//Then replace value of min with current node's data
if(min > current.data) {
min = current.data;
}
current= current.next;
}while(current != head);
System.out.println("Minimum value node in the list: "+ min);
}
}
//Finds out the maximum value node in the list
public void maxNode() {
Node current = head;
//Initializing max to initial node data
int max = head.data;
if(head == null) {
System.out.println("List is empty");
}
else {
do{
//If current node's data is greater than max
//Then replace value of max with current node's data
if(max < current.data) {
max = current.data;
}
current= current.next;
}while(current != head);
System.out.println("Maximum value node in the list: "+ max);
}
}
public static void main(String[] args) {
MinMax cl = new MinMax();
//Adds data to the list
cl.add(5);
cl.add(20);
cl.add(10);
cl.add(1);
//Prints the minimum value node in the list
cl.minNode();
//Prints the maximum value node in the list
cl.maxNode();
}
}
Output: Minimum value node in the list: 1 Maximum value node in the list: 20 C#
using System;
namespace CircularLinkedList
{
public class Program
{
//Represents the node of list.
public class Node<T>{
public T data;
public Node<T> next;
public Node(T value) {
data = value;
next = null;
}
}
public class CreateList<T> where T : IComparable<T> {
protected Node<T> head = null;
protected Node<T> tail = null;
//This function will add the new node at the end of the list.
public void add(T data){
//Create new node
Node<T> newNode = new Node<T>(data);
//Checks if the list is empty.
if(head == null){
head = newNode;
tail = newNode;
newNode.next = head;
}else{
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will point to head.
tail.next = head;
}
}
//Finds out the minimum value node in the list
public void minNode() {
Node<T> current = head;
//Initializing min to initial node data
T min = head.data;
if(head == null) {
Console.WriteLine("List is empty");
}
else {
do{
//If current node's data is smaller than min
//Then replace value of min with current node's data
if(min.CompareTo(current.data) > 0) {
min = current.data;
}
current= current.next;
}while(current != head);
Console.WriteLine("Minimum value node in the list: "+ min);
}
}
//Finds out the maximum value node in the list
public void maxNode() {
Node<T> current = head;
//Initializing max to initial node data
T max = head.data;
if(head == null) {
Console.WriteLine("List is empty");
}
else {
do{
//If current node's data is greater than max
//Then replace value of max with current node's data
if(max.CompareTo(current.data) < 0) {
max = current.data;
}
current= current.next;
}while(current != head);
Console.WriteLine("Maximum value node in the list: "+ max);
}
}
}
public static void Main()
{
CreateList<int> cl = new CreateList<int>();
//Adds data to the list
cl.add(5);
cl.add(20);
cl.add(10);
cl.add(1);
//Prints the minimum value node in the list
cl.minNode();
//Prints the maximum value node in the list
cl.maxNode();
}
}
}
Output: Minimum value node in the list: 1 Maximum value node in the list: 20 PHP
<!DOCTYPE html>
<html>
<body>
<?php
//Represents the node of list.
class Node{
public $data;
public $next;
function __construct($data){
$this->data = $data;
$this->next = NULL;
}
}
class CreateList{
//Declaring head and tail pointer as null.
private $head;
private $tail;
function __construct(){
$this->head = NULL;
$this->tail = NULL;
}
//This function will add the new node at the end of the list.
function add($data){
//Create new node
$newNode = new Node($data);
//Checks if the list is empty.
if($this->head == NULL){
//If list is empty, both head and tail would point to new node.
$this->head = $newNode;
$this->tail = $newNode;
$newNode->next = $this->head;
}
else{
//tail will point to new node.
$this->tail->next = $newNode;
//New node will become new tail.
$this->tail = $newNode;
//Since, it is circular linked list tail will point to head.
$this->tail->next = $this->head;
}
}
//Finds out the minimum value node in the list
function minNode() {
$current = $this->head;
//Initializing min to initial node data
$min = $this->head->data;
if($this->head == NULL) {
echo "List is empty";
}
else {
do{
//If current node's data is smaller than min
//Then replace value of min with current node's data
if($min > $current->data) {
$min = $current->data;
}
$current= $current->next;
}while($current != $this->head);
echo "Minimum value node in the list: $min <br>";
}
}
//Finds out the maximum value node in the list
function maxNode() {
$current = $this->head;
//Initializing max to initial node data
$max = $this->head->data;
if($this->head == NULL) {
echo "List is empty";
}
else {
do{
//If current node's data is greater than max
//Then replace value of max with current node's data
if($max < $current->data) {
$max = $current->data;
}
$current= $current->next;
}while($current != $this->head);
echo "Maximum value node in the list: $max <br>";
}
}
}
$cl = new CreateList();
//Adds data to the list
$cl->add(5);
$cl->add(20);
$cl->add(10);
$cl->add(1);
//Prints the minimum value node in the list
$cl->minNode();
//Prints the maximum value node in the list
$cl->maxNode();
?>
</body>
</html>
Output: Minimum value node in the list: 1 Maximum value node in the list: 20
Next Topic#
|