Program to Find the Largest Element in a Doubly Linked List

  • Write a C program to find maximum element of a doubly linked list.
  • Function to print largest element of a doubly linked list.

Given a doubly linked list, we have to find maximum value node of a doubly linked list. For Example:

Doubly Linked List
2<-->5<-->9<-->1<-->10<-->1
Maximum Element : 10
In this program, we will use iteration to traverse doubly linked list and search for maximum element.
Algorithm to find largest node in doubly linked list
Let "head" be the pointer to the head node of given doubly linked list.
  • Initialize a variable "max" with data of head node.(max = head->data;)
  • Using a loop, traverse doubly linked list until "head" is not equal to NULL.
  • For every node, if it's value is larger than max then set max = head->data; else continue.
  • At the end of traversal, max will contain the maximum value of given doubly linked list.

In this program, we will use a user defined function "getMaxNode" which takes head pointer of linked list and returns the value of largest node.

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;
}

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