Write a program in C to find the minimum value from a doubly linked list

Q. Program to find the maximum and minimum value node from a doubly linked list.

Explanation

In this program, we will create a doubly linked list then, iterate through the list to find out the minimum and maximum node.

Write a program in C to find the minimum value from a doubly linked list

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, 1 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 will have three properties: data, previous which will point to the previous node and next which will point to the next node.
  2. Define another class for creating the doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
  3. minimumNode() will prints 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. maximumNode() 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.

C Exercises: Find maximum value from a doubly linked list

Last update on December 20 2021 09:10:13 (UTC/GMT +8 hours)

Find minimum in doubly linked list by R4R Team

    traverse() function are used to get all data from the doubly linked list.
    minimum() function are used to find the minimum element in the doubly linked list.

    program-

    #include< stdio.h>
    #include< conio.h>
    #include< alloc.h>
    struct node{
    struct node *pre;
    struct node *next;
    int data;
    };
    struct node *first=NULL,*last=NULL;
    void minimum()
    {
    struct node *ptr=first;
    int min=ptr->data;
    while(ptr!=NULL)
    {
    if(ptr->data< min)
    min=ptr->data;
    ptr=ptr->next;
    }
    printf("nMinimum is %d",min);
    }
    void traverse()
    {
    struct node *ptr=first;
    while(ptr!=NULL){
    printf("%d ",ptr->data);
    ptr=ptr->next;
    }
    }
    void main()
    {
    struct node *cpt,*ptr;
    char ch;
    clrscr();
    first=(struct node*)malloc(sizeof(struct node));
    printf("Enter first element in Linklist\n");
    scanf("%d",&first->data);
    first->pre=NULL;
    ptr=first;
    do
    {
    cpt=(struct node*)malloc(sizeof(struct node));
    printf("Enter another data\n");
    scanf("%d",&cpt->data);
    ptr->next=cpt;
    cpt->pre=ptr;
    ptr=ptr->next;
    printf("Continue(y/n) ?");
    ch=getch();
    }while(ch=='y');
    ptr->next=NULL;
    last=ptr;
    printf("nDoubly Linked-list is:\n");
    traverse();
    minimum();
    getch();
    }


    output-

    Enter first element in Linklist
    4
    Enter another data
    5
    Continue(y/n)?
    Enter another data
    1
    Continue(y/n)?
    Enter another data
    9
    Continue(y/n)?
    Enter another data
    0
    Continue(y/n)?
    Doubly Linked-List is
    4 5 1 9 0
    Minimum is 0




    Leave a Comment:
    Submit
    Search
    Go!
    Categories
    • Introduction to Linked list in Data structure
    • Create a Doubly linked list
    • Traverse the Doubly Linked list
    • Insertion at beginning in doubly linked list
    • Insertion at end in Doubly linked list
    • Insertion at given position in doubly linked list
    • Deletion at beginning in Doubly Linked list
    • Deletion at end in Doubly Linked list
    • Deletion at any position in doubly linked list
    • Traverse Doubly linked list in reverse order
    • Traverse Doubly linked list by recursion
    • Find maximum in doubly linked list
    • Find minimum in doubly linked list
    • Stack implementation by linked list
    • Queue implementation by Doubly linked list
    • Find average of doubly linked list
    • Find second last element of doubly linked list
    • Find length of the doubly linked list
    • Insertion in sorted Doubly linked list
    • Add two Doubly linked list
    • Concate two doubly linked list
    • Singly Linked List Data Structure in C
    • Circular Linked List in data Structure
    • Doubly Linked List in data structure
    • Array Data structure
    • String Data structure
    • Stack Implementation by array
    • Stack implementation by Linked list
    • Linear Queue Implemented by Array
    • Queue Implementation by linked list
    • Searching
    • Sorting Algorithm in Data structure
    • Circular Queue in Data Structure
    • Tree & Graph Data Structure
    • Binary Search Tree in Data structure
    • Complexity in Data Structure
    • Core Java
    • Core Java Interview Question Answers
    • Hibernate
    • Hibernate Interview Question Answers
    • Servlet
    • Servlet Interview Question Answers
    • MYSQL
    • MYSQL Interview Question Answers
    • JavaServer Pages (JSP)
    • JavaServer Pages (JSP) Interview Question Answers
    • Spring
    • Spring Interview Question Answers
    • Struts 2
    • Struts 2 Interview Question Answers
    • J2ME
    • J2ME Interview Question Answers
    • General Knowledge
    • General Knowledge Interview Question Answers
    • Spring boot
    • Spring boot Interview Question Answers
    • Python
    • Python Interview Question Answers
    • c language
    • c language Interview Question Answers
    • C++ language
    • C++ language Interview Question Answers
    • Data Structure using c
    • Data Structure using c Interview Question Answers
    R4R Team
    R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!

    Find smallest and largest elements in singly linked list

    Given a singly linked list of n nodes and find the smallest and largest elements in linked list.
    Examples:

    Input : 15 14 13 22 17 Output : Linked list are: 17 -> 22 -> 13 -> 14 -> 15 -> NULL Maximum element in linked list: 22 Minimum element in linked list: 13 Input : 20 25 23 68 54 13 45 Output : Linked list are: 45 -> 13 -> 54 -> 68 -> 23 -> 25 -> 20 -> NULL Maximum element in linked list: 68 Minimum element in linked list: 13

    Recommended: Please try your approach on {IDE} first, before moving on to the solution.

    The idea is to traverse the linked list while head not equal to NULL and initialise the max and min variable to INT_MIN and INT_MAX respectively. After that check a condition that if max value is less then head value is assigned to max or min value is greater then head value is assigned to min otherwise head point to next node. Continue this process until head not equal to NULL.




    // C++ Program to find smallest and largest
    // elements in singly linked list.
    #include <bits/stdc++.h>
    using namespace std;
    /* Linked list node */
    struct Node {
    int data;
    struct Node* next;
    };
    // Function that returns the largest element
    // from the linked list.
    int largestElement(struct Node* head)
    {
    // Declare a max variable and initialize
    // it with INT_MIN value.
    // INT_MIN is integer type and its value
    // is -32767 or less.
    int max = INT_MIN;
    // Check loop while head not equal to NULL
    while (head != NULL) {
    // If max is less then head->data then
    // assign value of head->data to max
    // otherwise node point to next node.
    if (max < head->data)
    max = head->data;
    head = head->next;
    }
    return max;
    }
    // Function that returns smallest element
    // from the linked list.
    int smallestElement(struct Node* head)
    {
    // Declare a min variable and initialize
    // it with INT_MAX value.
    // INT_MAX is integer type and its value
    // is 32767 or greater.
    int min = INT_MAX;
    // Check loop while head not equal to NULL
    while (head != NULL) {
    // If min is greater then head->data then
    // assign value of head->data to min
    // otherwise node point to next node.
    if (min > head->data)
    min = head->data;
    head = head->next;
    }
    return min;
    }
    // Function that push the element in linked list.
    void push(struct Node** head, int data)
    {
    // Allocate dynamic memory for newNode.
    struct Node* newNode =
    (struct Node*)malloc(sizeof(struct Node));
    // Assign the data into newNode.
    newNode->data = data;
    // newNode->next assign the address of
    // head node.
    newNode->next = (*head);
    // newNode become the headNode.
    (*head) = newNode;
    }
    // Display linked list.
    void printList(struct Node* head)
    {
    while (head != NULL) {
    printf("%d -> ", head->data);
    head = head->next;
    }
    cout << "NULL" << endl;
    }
    // Driver program to test the functions
    int main()
    {
    // Start with empty list
    struct Node* head = NULL;
    // Using push() function to construct
    // singly linked list
    // 17->22->13->14->15
    push(&head, 15);
    push(&head, 14);
    push(&head, 13);
    push(&head, 22);
    push(&head, 17);
    cout << "Linked list is : " << endl;
    // Call printList() function to display
    // the linked list.
    printList(head);
    cout << "Maximum element in linked list:";
    // Call largestElement() function to get largest
    // element in linked list.
    cout << largestElement(head) << endl;
    cout << "Minimum element in linked list:";
    // Call smallestElement() function to get smallest
    // element in linked list.
    cout << smallestElement(head) << endl;
    return 0;
    }




    // Java program to find smallest and largest
    // elements in singly linked list.
    class GfG
    {
    /* Linked list node */
    static class Node
    {
    int data;
    Node next;
    }
    static Node head = null;
    // Function that returns the largest element
    // from the linked list.
    static int largestElement(Node head)
    {
    // Declare a max variable and initialize
    // it with INT_MIN value.
    // INT_MIN is integer type and its value
    // is -32767 or less.
    int max = Integer.MIN_VALUE;
    // Check loop while head not equal to NULL
    while (head != null)
    {
    // If max is less then head->data then
    // assign value of head->data to max
    // otherwise node point to next node.
    if (max < head.data)
    max = head.data;
    head = head.next;
    }
    return max;
    }
    // Function that returns smallest element
    // from the linked list.
    static int smallestElement(Node head)
    {
    // Declare a min variable and initialize
    // it with INT_MAX value.
    // INT_MAX is integer type and its value
    // is 32767 or greater.
    int min = Integer.MAX_VALUE;
    // Check loop while head not equal to NULL
    while (head != null)
    {
    // If min is greater then head->data then
    // assign value of head->data to min
    // otherwise node point to next node.
    if (min > head.data)
    min = head.data;
    head = head.next;
    }
    return min;
    }
    // Function that push the element in linked list.
    static void push(int data)
    {
    // Allocate dynamic memory for newNode.
    Node newNode = new Node();
    // Assign the data into newNode.
    newNode.data = data;
    // newNode->next assign the address of
    // head node.
    newNode.next = (head);
    // newNode become the headNode.
    (head) = newNode;
    }
    // Display linked list.
    static void printList(Node head)
    {
    while (head != null) {
    System.out.print(head.data + " -> ");
    head = head.next;
    }
    System.out.println("NULL");
    }
    // Driver code
    public static void main(String[] args)
    {
    // Start with empty list
    // head = new Node();
    // Using push() function to construct
    // singly linked list
    // 17->22->13->14->15
    push( 15);
    push( 14);
    push( 13);
    push( 22);
    push( 17);
    System.out.println("Linked list is : ") ;
    // Call printList() function to
    // display the linked list.
    printList(head);
    System.out.print("Maximum element in linked list: ");
    // Call largestElement() function to get
    // largest element in linked list.
    System.out.println(largestElement(head));
    System.out.print("Minimum element in linked list: ");
    // Call smallestElement() function to get
    // smallest element in linked list.
    System.out.print(smallestElement(head));
    }
    }
    // This code is contributed by Prerna saini.




    # Python3 program to find smallest and largest
    # elements in singly linked list.
    # Linked list node
    class Node:
    def __init__(self):
    self.data = None
    self.next = None
    head = None
    # Function that returns the largest element
    # from the linked list.
    def largestElement(head):
    # Declare a max variable and initialize
    # it with INT_MIN value.
    # INT_MIN is integer type and its value
    # is -32767 or less.
    max = -32767
    # Check loop while head not equal to None
    while (head != None):
    # If max is less then head.data then
    # assign value of head.data to max
    # otherwise node point to next node.
    if (max < head.data) :
    max = head.data
    head = head.next
    return max
    # Function that returns smallest element
    # from the linked list.
    def smallestElement(head):
    # Declare a min variable and initialize
    # it with INT_MAX value.
    # INT_MAX is integer type and its value
    # is 32767 or greater.
    min = 32767
    # Check loop while head not equal to None
    while (head != None) :
    # If min is greater then head.data then
    # assign value of head.data to min
    # otherwise node point to next node.
    if (min > head.data) :
    min = head.data
    head = head.next
    return min
    # Function that push the element in linked list.
    def push( data) :
    global head
    # Allocate dynamic memory for newNode.
    newNode = Node()
    # Assign the data into newNode.
    newNode.data = data
    # newNode.next assign the address of
    # head node.
    newNode.next = (head)
    # newNode become the headNode.
    (head) = newNode
    # Display linked list.
    def printList( head) :
    while (head != None) :
    print(head.data ,end= " -> ")
    head = head.next
    print("None")
    # Driver code
    # Start with empty list
    # head = new Node()
    # Using push() function to construct
    # singly linked list
    # 17.22.13.14.15
    push( 15)
    push( 14)
    push( 13)
    push( 22)
    push( 17)
    print("Linked list is : ")
    # Call printList() function to
    # display the linked list.
    printList(head)
    print("Maximum element in linked list: ",end="")
    # Call largestElement() function to get
    # largest element in linked list.
    print(largestElement(head))
    print("Minimum element in linked list: ",end="")
    # Call smallestElement() function to get
    # smallest element in linked list.
    print(smallestElement(head),end="")
    # This code is contributed by Arnab Kundu




    // C# program to find smallest and largest
    // elements in singly linked list.
    using System;
    class GfG
    {
    /* Linked list node */
    class Node
    {
    public int data;
    public Node next;
    }
    static Node head = null;
    // Function that returns the largest element
    // from the linked list.
    static int largestElement(Node head)
    {
    // Declare a max variable and initialize
    // it with INT_MIN value.
    // INT_MIN is integer type and its value
    // is -32767 or less.
    int max = int.MinValue;
    // Check loop while head not equal to NULL
    while (head != null)
    {
    // If max is less then head->data then
    // assign value of head->data to max
    // otherwise node point to next node.
    if (max < head.data)
    max = head.data;
    head = head.next;
    }
    return max;
    }
    // Function that returns smallest element
    // from the linked list.
    static int smallestElement(Node head)
    {
    // Declare a min variable and initialize
    // it with INT_MAX value.
    // INT_MAX is integer type and its value
    // is 32767 or greater.
    int min = int.MaxValue;
    // Check loop while head not equal to NULL
    while (head != null)
    {
    // If min is greater then head->data then
    // assign value of head->data to min
    // otherwise node point to next node.
    if (min > head.data)
    min = head.data;
    head = head.next;
    }
    return min;
    }
    // Function that push the element in linked list.
    static void push(int data)
    {
    // Allocate dynamic memory for newNode.
    Node newNode = new Node();
    // Assign the data into newNode.
    newNode.data = data;
    // newNode->next assign the address of
    // head node.
    newNode.next = (head);
    // newNode become the headNode.
    (head) = newNode;
    }
    // Display linked list.
    static void printList(Node head)
    {
    while (head != null)
    {
    Console.Write(head.data + " -> ");
    head = head.next;
    }
    Console.WriteLine("NULL");
    }
    // Driver code
    public static void Main()
    {
    // Start with empty list
    // head = new Node();
    // Using push() function to construct
    // singly linked list
    // 17->22->13->14->15
    push( 15);
    push( 14);
    push( 13);
    push( 22);
    push( 17);
    Console.WriteLine("Linked list is : ") ;
    // Call printList() function to
    // display the linked list.
    printList(head);
    Console.Write("Maximum element in linked list: ");
    // Call largestElement() function to get
    // largest element in linked list.
    Console.WriteLine(largestElement(head));
    Console.Write("Minimum element in linked list: ");
    // Call smallestElement() function to get
    // smallest element in linked list.
    Console.Write(smallestElement(head));
    }
    }
    // This code is contributed by PrinciRaj1992




    <script>
    // JavaScript program to find smallest and largest
    // elements in singly linked list.
    /* Linked list node */
    class Node {
    constructor(val) {
    this.data = val;
    this.next = null;
    }
    }
    var head = null;
    // Function that returns the largest element
    // from the linked list.
    function largestElement(head) {
    // Declare a max variable and initialize
    // it with INT_MIN value.
    // INT_MIN is integer type and its value
    // is -32767 or less.
    var max = Number.MIN_VALUE;
    // Check loop while head not equal to NULL
    while (head != null) {
    // If max is less then head->data then
    // assign value of head->data to max
    // otherwise node point to next node.
    if (max < head.data)
    max = head.data;
    head = head.next;
    }
    return max;
    }
    // Function that returns smallest element
    // from the linked list.
    function smallestElement(head) {
    // Declare a min variable and initialize
    // it with INT_MAX value.
    // INT_MAX is integer type and its value
    // is 32767 or greater.
    var min = Number.MAX_VALUE;
    // Check loop while head not equal to NULL
    while (head != null) {
    // If min is greater then head->data then
    // assign value of head->data to min
    // otherwise node point to next node.
    if (min > head.data)
    min = head.data;
    head = head.next;
    }
    return min;
    }
    // Function that push the element in linked list.
    function push(data) {
    // Allocate dynamic memory for newNode.
    var newNode = new Node();
    // Assign the data into newNode.
    newNode.data = data;
    // newNode->next assign the address of
    // head node.
    newNode.next = (head);
    // newNode become the headNode.
    (head) = newNode;
    }
    // Display linked list.
    function printList(head) {
    while (head != null) {
    document.write(head.data + " -> ");
    head = head.next;
    }
    document.write("NULL");
    }
    // Driver code
    // Start with empty list
    // head = new Node();
    // Using push() function to construct
    // singly linked list
    // 17->22->13->14->15
    push(15);
    push(14);
    push(13);
    push(22);
    push(17);
    document.write("Linked list is : <br/>");
    // Call printList() function to
    // display the linked list.
    printList(head);
    document.write("<br/>Maximum element in linked list: ");
    // Call largestElement() function to get
    // largest element in linked list.
    document.write(largestElement(head));
    document.write("<br/>Minimum element in linked list: ");
    // Call smallestElement() function to get
    // smallest element in linked list.
    document.write(smallestElement(head));
    // This code contributed by Rajput-Ji
    </script>

    Output:

    Linked list is : 17 -> 22 -> 13 -> 14 -> 15 -> NULL Maximum element in linked list: 22 Minimum element in linked list: 13

    This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    Write a program in C to find the minimum value from a doubly linked list




    Article Tags :
    Linked List
    Practice Tags :
    Linked List

    C program to print largest nodes of a doubly linked list

    #include <stdio.h> #include <stdlib.h> #include <limits.h> /* A structure of Doubly linked list node */ struct node { int data; struct node *prev, *next; } *head; void initialize(){ head = NULL; } /* Given a Inserts a node in front of a doubly linked list. */ void insert(int num) { /* Create a new Linked List node */ struct node* newNode = (struct node*) malloc(sizeof(struct node)); newNode->data = num; /* Insert newNode before head node */ newNode->next = head; newNode->prev = NULL; if(head != NULL) { head->prev = newNode; } /* Set newNode as head of doubly linked list */ head = newNode; printf("Inserted Element : %d\n", num); } int getMaxNode(struct node *head){ /* Input Validation */ if(head == NULL){ printf("Error : Invalid Input !!!!\n"); return INT_MIN; } int max = head->data; while(head != NULL){ if(head->data > max){ max = head->data; } head = head->next; } return max; } /* Prints a linked list from head node till tail node */ void printLinkedList(struct node *nodePtr) { printf("Doubly Linked List\n"); while (nodePtr != NULL) { printf("%d", nodePtr->data); nodePtr = nodePtr->next; if(nodePtr != NULL) printf("-><-"); } } int main() { initialize(); /* Insert elements in Doubly linked list */ insert(4); insert(9); insert(12); insert(1); /* print Doubly Linked list */ printLinkedList(head); /* Printing maximum value node*/ printf("\nMaximum Value in Linked List : %d", getMaxNode(head)); return 0; } Output
    Inserted Element : 4 Inserted Element : 9 Inserted Element : 12 Inserted Element : 1 Doubly Linked List 1-><-12-><-9-><-4 Maximum Value in Linked List : 12

    Python program to find the maximum and minimum value node from a doubly linked list

    PythonServer Side ProgrammingProgramming

    When it is required to find the maximum and minimum values from a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.

    Below is a demonstration for the same −