Javatpoint Logo
Javatpoint Logo

Program to find the maximum and minimum value node from a circular linked list

Explanation

In 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

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list and it has two nodes: head and tail.
  3. minNode() will print out minimum value node:
    1. Define variable min and initialize with head's data.
    2. Current will point to head.
    3. Iterate through the list by comparing each node's data with min.
    4. If min > current's data then min will hold current's data.
    5. At the end of the list, variable min will hold the minimum value node.
    6. Print the min value.
  4. maxNode() will prints out maximum value node:
    1. Define variable max and initialize with head's data.
    2. Current will point to head.
    3. Iterate through the list by comparing each node's data with max.
    4. If max > current's data then max will hold current's data.
    5. At the end of the list, variable max will hold the maximum value node.
    6. Print the max value.

Solution

Python

Output:

Minimum value node in the list: 1
Maximum value node in the list: 20

C

Output:

Minimum value node in the list: 1
Maximum value node in the list: 20

JAVA

Output:

Minimum value node in the list: 1
Maximum value node in the list: 20

C#

Output:

Minimum value node in the list: 1
Maximum value node in the list: 20

PHP

Output:

Minimum value node in the list: 1
Maximum value node in the list: 20

Next Topic#




Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials