Description
Finding maximum and minimum element of singly linked list - 1. How to find maximum element Step 1: Storing maximum value into one variable. Step 2: Traversing the loop and comparing the value of list with maximum value stored in variable. Step 3: If any value from list is greater than the value stored in variable, update the variable with maximum value from list. 2. How to find minimum element Step 1: Storing minimum value into one variable. Step 2: Traversing the loop and comparing the value of list with minimum value stored in variable. Step 3: If any value from list is lesser than the value stored in variable, update the variable with minimum value from list. Note: Here head contains the address of first node
C/C++
/* C Program to Find Maximum And Minimum Value Node In Singly Linked List */ //Save it as max_min_singly_linked_list.c #include<stdio.h> #include<malloc.h> #include<limits.h> struct node *create(int); void display(struct node *); void find_max(struct node *); //Declaring node struct node{ int data; struct node *next; }; int main(){ int n; struct node *head; printf("Enter number of nodes : "); scanf("%d",&n); //Calling function to create node head=create(n); //Calling function to display list display(head); //Function to call maximum value find_max(head); //Function to call minimum value find_min(head); return 0; } struct node *create(int n){ int i; struct node *head=NULL; struct node *address,*newNode; for(i=0;i<n;i++){ //Creating a new node newNode=(struct node*)malloc(sizeof(struct node*)); //Assigning data to newly created node printf("Enter the value of %d node : ",i+1); scanf("%d",&newNode->data); newNode->next=NULL; /*If list is empty assign the address of newly created node to head*/ if(head==NULL){ head=newNode; }else{ /* If list already have few elements then loop through list and add newly created node after given node*/ address=head; while(address->next!=NULL){ address=address->next; } address->next=newNode; } } return head; } void display(struct node *head){ struct node *address; //If list is empty if(head==NULL){ printf("The linked list is empty"); }else{ /*If list has elements then loop through the loop and print elements one by one in sequential manner*/ address=head; while(address!=NULL){ printf("The node value = %d and their address = %u\n",address->data,address); address=address->next; } } } //Function for finding maximum void find_max(struct node *head){ struct node *address, *maxValAddress; int max_val=INT_MIN; address=head; //If list has no any element if(head==NULL){ printf("\nLinked List is empty..Cannot find maximum element"); }else{ //Traversing the list while(address!=NULL){ //Checking list that any element is greater or not if(address->data > max_val){ //Assigning the maximum value of list into variable if found max_val=address->data; //Storing the address of maximum value maxValAddress=address; } address=address->next; } printf("\nThe maximum value = %d and it's address = %u",max_val,maxValAddress); } } //Function for finding minimum void find_min(struct node *head){ struct node *address, *minValAddress; int min_val=INT_MAX; address=head; //If list has no any element if(head==NULL){ printf("\nLinked List is empty..Cannot find minimum element"); }else{ //Traversing the list while(address!=NULL){ //Checking list that any element is lesser or not if(address->data < min_val){ //Assigning the minimum value of list into variable if found min_val=address->data; //Storing the address of minimum value minValAddress=address; } address=address->next; } printf("\nThe minimum value = %d and it's address = %u",min_val,minValAddress); } }
Input: Enter number of nodes : 3 Enter the value of 1 node : 1 Enter the value of 2 node : 5 Enter the value of 3 node : 9 Output: The node value = 1 and their address = 6753608 The node value = 5 and their address = 6753640 The node value = 9 and their address = 6753656 The maximum value = 9 and it's address = 6753656 The minimum value = 1 and it's address = 6753608
Java
/* Java Program to Find Maximum And Minimum Value Node In Singly Linked List */ //Save it as MaxMinSinglyLinkedList.java import java.io.*; import java.util.Scanner; public class MaxMinSinglyLinkedList { // Declaring node class Node { int data; Node next; public Node(int data) { this.data = data; // Assigning null value to the next field this.next = null; } } public static Node head = null; // Function to add node public void addNode(int data) { // Creating a new node Node newNode = new Node(data); /* If list is empty assign the address of newly created node to head */ if (head == null) { head = newNode; } else { // If list already have few elements then loop through list and Node ptr = head; while (ptr.next != null) { ptr = ptr.next; } ptr.next = newNode; } } public void display() { Node ptr = head; // If list is empty if (ptr == null) { System.out.println("The list is empty"); return; } System.out.println("The nodes of lists are : "); /* * If list has elements then loop through the loop and print elements one by one * in sequential manner */ while (ptr != null) { System.out.print(ptr.data + " "); ptr = ptr.next; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); MaxMinSinglyLinkedList list = new MaxMinSinglyLinkedList(); System.out.println("Enter number of node you want to enter : "); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { System.out.println("Enter value of " + (i + 1) + " node : "); int val = scanner.nextInt(); list.addNode(val); } // Calling function to display list list.display(); // Function to call maximum value find_max(); // Function to call minimum value find_min(); } // Method for finding maximum private static void find_max() { Node address; int max_val = Integer.MIN_VALUE; address = head; // If list has no any element if (head == null) { System.out.println("Linked List is empty..Cannot find maximum element"); } else { // Traversing the list while (address != null) { // Checking list that any element is greater or not if (address.data > max_val) { // Assigning the maximum value of list into variable if found max_val = address.data; } address = address.next; } System.out.println("\n\nThe maximum value : " + max_val); } } // Method for finding minimum private static void find_min() { Node address; int min_val = Integer.MAX_VALUE; address = head; // If list has no any element if (head == null) { System.out.println("Linked List is empty..Cannot find minimum element"); } else { // Traversing the list while (address != null) { // Checking list that any element is lesser or not if (address.data < min_val) { // Assigning the minimum value of list into variable if found min_val = address.data; } address = address.next; } System.out.println("The minimum value : " + min_val); } } }
Input: Enter number of node you want to enter : 5 Enter value of 1 node : 6 Enter value of 2 node : 4 Enter value of 3 node : 2 Enter value of 4 node : 7 Enter value of 5 node : 3 Output: The nodes of lists are : 6 4 2 7 3 The maximum value : 7 The minimum value : 2
Related Programs
1) Program To Create And Display Singly Linked List2) Program To Add A Node In Beginning Of Singly Linked List
3) Program To Add A Node In The End Of Singly Linked List
4) Program To Add A Node Before A Given Node Singly Linked List
5) Program To Add A Node After A Given Node Singly Linked List
6) Program To Delete Node From Beginning Singly Linked List
7) Program To Delete Node From End Singly Linked List
8) Program To Delete Given Node Singly Linked List
9) Program To Delete Node Before Given Node Singly Linked List
10) Program To Delete Node After Given Node Singly Linked List
11) Program To Delete All Nodes Of Singly Linked List
12) Program To Sort Elements Of Singly Linked List
13) Program To Reverse Elements Singly Linked List
14) Program to Search an Element In Singly Linked List
15) Program To Remove Duplicate Elements From Singly Linked List
16) Program To Create And Display Circular Singly Linked List
17) Program To Add A Node At The Beginning Of Circular Linked List