Q. Program to find the maximum and minimum value node from a doubly linked list.ExplanationIn this program, we will create a doubly linked list then, iterate through the list to find out the minimum and maximum node. Show
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
C Exercises: Find maximum value from a doubly linked listLast update on December 20 2021 09:10:13 (UTC/GMT +8 hours)Find minimum in doubly linked list by R4R Teamtraverse() function are used to get all data from the doubly linked list. #include< stdio.h> output- Enter first element in Linklist Leave a Comment:Submit Search Go! Categories
R4R TeamR4Rin 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 listGiven a singly linked list of n nodes and find the smallest and largest elements in linked list. 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++
Java
Python
C#
Javascript
Output: 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.
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; } OutputInserted 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 listPythonServer 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 − |