Home » C programs

Find the largest element in a doubly linked list | C program

Here, we are going to learn how to find the largest element in a doubly linked list using C program?
Submitted by Piyas Mukherjee, on January 31, 2019

Algorithm:

Function: largest_DLL()

Input: A doubly linked list whose address of the first node is stored in a pointer say head

Output: The largest no of the linked list say max

Data structure used: A doubly linked list where each node contains a data part, say data and two link parts say prev (to store the address of immediate previous node) and say next (to store the address of immediate next node).

Pseudo code:

Begin
temp=head
max=temp->data //max to store maximum
while(temp!=NULL)
begin
	if(temp->data>max)
		max=temp->data
		temp=temp->next
	end if
end while
End

C code:

#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
	int data; 
	struct list *next;
	struct list *prev;
}node;

void display(node *temp)
{
	printf("The list is as follows :\n");
	while(temp!=NULL)
	{
		printf("%d->",temp->data);
		temp=temp->next;
	}
	printf("NULL");
	return;
}

int main(){
	node *head=NULL,*temp,*temp1;
	int choice,max;
	
	//Taking the linked list as input
	do
	{
		temp=(node *)malloc(sizeof(node));
		if(temp!=NULL)
		{
			printf("\nEnter the element in the list : ");
			scanf("%d",&temp->data);
			temp->next=NULL;
			if(head==NULL)
			{	
				temp->prev=head;
				temp->next=head;
				head=temp;
			}
			else
			{
				temp1=head;
				while(temp1->next!=NULL)
				{
					temp1=temp1->next;
				}
				temp1->next=temp;
				temp->prev=temp1;
			}
		}
		else
		{
			printf("\nMemory not avilable...node allocation is not possible");
		}
		printf("\nIf you wish to add more data on the list enter 1 : ");
		scanf("%d",&choice);
	}while(choice==1);
	
	display(head);
	
	//finding max
	temp=head;
	max=temp->data;
	while(temp!=NULL)
	{
		if(temp->data>max)
			max=temp->data;
		temp=temp->next;
	}
	printf("\nThe largest element in the list is : %d",max);
	
	return 0;
}

Output

Enter the element in the list : 1

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 2

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : -3

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 56

If you wish to add more data on the list enter 1 : 0
The list is as follows :
1->2->-3->56->NULL
The largest element in the list is : 56
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.