Example-
linked list is 2->3->6->1->0
Max is 6
Main code:
void maximum(struct node *ptr)
{
int max=ptr->data;
while(ptr!=NULL)
{
if(ptr->data>max)
max=ptr->data;
ptr=ptr->link;
}
printf("Maximum in linklist is %d",max);
}
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node{
struct node *link;
int data;
};
struct node *first=NULL;
void maximum(struct node *ptr)
{
int max=ptr->data;
while(ptr!=NULL)
{
if(ptr->data>max)
max=ptr->data;
ptr=ptr->link;
}
printf("Maximum in linklist is %d",max);
}
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);
ptr=first;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter another data\n");
scanf("%d",&cpt->data);
ptr->link=cpt;
ptr=ptr->link;
printf("Continue(y/n) ?");
ch=getch();
}while(ch=='y');
ptr->link=NULL;
maximum(first);
getch();
}
Enter first element in Linked list
2
Enter another data
4
Continue(y/n)?
Enter another data
1
Continue(y/n) ?
Enter another data
9
Continue(y/n) ?
Enter another data
0
Continue(y/n) ?
Maximum in Linked list is 9