C Program to Find the Largest Element in a Doubly Linked List

This C Program finds the largest element in a doubly linked list.

Here is a source code of the C Program that finds the largest in a doubly linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find the Largest Element in a Doubly Linked List 
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int num;
  11.     struct node *next;
  12.     struct node *prev;
  13. };
  14.  
  15. void create(struct node **);
  16. int max(struct node *);
  17. void release(struct node **);
  18.  
  19. int main()
  20. {
  21.     struct node *p = NULL;
  22.     int n;
  23.  
  24.     printf("Enter data into the list\n");
  25.     create(&p);
  26.     n = max(p);
  27.     printf("The maximum number entered in the list is %d.\n", n);
  28.     release (&p);
  29.  
  30.     return 0;
  31. }
  32.  
  33. int max(struct node *head)
  34. {
  35.     struct node *max, *q;
  36.  
  37.     q = max = head;
  38.     while (q != NULL)
  39.     {
  40.         if (q->num > max->num)
  41.         {
  42.             max = q;
  43.         }
  44.         q = q->next;
  45.     }
  46.  
  47.     return (max->num);
  48. }
  49.  
  50. void create(struct node **head)
  51. {
  52.     int c, ch;
  53.     struct node *temp, *rear;
  54.  
  55.     do
  56.     {
  57.         printf("Enter number: ");
  58.         scanf("%d", &c);
  59.         temp = (struct node *)malloc(sizeof(struct node));
  60.         temp->num = c;
  61.         temp->next = NULL;
  62.         temp->prev = NULL;
  63.         if (*head == NULL)
  64.         {
  65.             *head = temp;
  66.         }
  67.         else
  68.         {
  69.             rear->next = temp;
  70.             temp->prev = rear;
  71.         }
  72.         rear = temp;
  73.         printf("Do you wish to continue [1/0]: ");
  74.         scanf("%d", &ch);
  75.     } while (ch != 0);
  76.     printf("\n");
  77. }
  78.  
  79. void release(struct node **head)
  80. {
  81.     struct node *temp = *head;
  82.     *head = (*head)->next;
  83.     while ((*head) != NULL)
  84.     {
  85.         free(temp);
  86.         temp = *head;
  87.         (*head) = (*head)->next;
  88.     }
  89. }

$ cc largestdoubly.c 
$ ./a.out
Enter data into the list
Enter number: 12
Do you wish to continue [1/0]: 1
Enter number: 7
Do you wish to continue [1/0]: 1
Enter number: 23
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 16
Do you wish to continue [1/0]: 0
 
The maximum number entered in the list is 23.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.