F Find Which Node Has The Largest And Smallest Value In A Link List | CodeTheta

Find Which Node Has The Largest And Smallest Value In A Link List

May 10, 2014

Here you can see that it is link list program that will calculate the largest and smallest value in the link list. The out put of this program you can see in below picture.


/* http://native-code.blogspot.com */

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct link
{
int data;
struct link *next;
} node;
node *ins_beg(node*, int);
void display(node*);
void findLargestNode(node *h);
void findSmallestNode(node *h);
void main()
{
int x, ch;
node *head=NULL;
while(1)
{
printf("/* http://native-code.blogspot.com */");
printf("\n1-->Insert at first\n2-->Display\n3-->Find largest node");
printf("\n4-->Find smallest node \n5-->EXIT\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter data of the node:\n");
scanf("%d", &x);
head=ins_beg(head, x);
break;
case 2:
display(head);
printf("\n");
break;
case 3:
findLargestNode(head);
break;
case 4:
findSmallestNode(head);
break;
case 5:

default:
printf("Wrong choice.\n");
}
}
}
node *ins_beg(node *h, int x)
{
node *temp;
temp=(node*) malloc (sizeof(node));
temp->data=x;
temp->next=NULL;
if(h==NULL)
{
h=temp;
}
else
{
temp->next=h;
h=temp;
}
return(h);
}
void display( node *h)
{
node *r=h;
while(r!=NULL)
{
printf("%d-->", r->data);
r=r->next;
}
printf("NULL");
}
void findLargestNode(node *h)
{
int l=h->data;
node *r=h;
while(r!=NULL)
{ if(r->data>=l)
l=r->data;
r=r->next;
}
printf("Largest Node: %d\n", l);
}
void findSmallestNode(node *h)
{
int s=h->data;
node *r=h;
while(r!=NULL)
{ if(r->data<=s)
s=r->data;
r=r->next;
}
printf("Smallest Node: %d\n", s);
}
/* http://native-code.blogspot.com */

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us

Post a Comment