Create a single linked list & print the largest element from the list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *start;
main()
{
void create();
void show();
int max_item();
start=NULL;
create();
printf("\nThe Linked List :\n");
show();
printf("\nThe largest element=%d",max_item());
getch();
}
void create()
{
node *ptr,*temp,*new_node;
int n,val,i;
printf("\nEnter the no. of nodes :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("\nOverflow");
return;
}
printf("\nEnter the value for node %d :",i);
scanf("%d",&val);
new_node->info=val;
new_node->next=NULL;
if(start==NULL)
{
ptr=new_node;
start=ptr;
}
else
{
ptr->next=new_node;
ptr=ptr->next;
}
}
return;
}
void show()
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->next==NULL)
printf("%d",ptr->info);
else
printf("%d-->",ptr->info);
ptr=ptr->next;
}
return;
}
int max_item()
{
node *ptr;
int max;
ptr=start;
max=ptr->info;
ptr=ptr->next;
while(ptr!=NULL)
{
if(ptr->info > max)
{
max=ptr->info;
}
ptr=ptr->next;
}
return(max);
}
#include<conio.h>
#include<stdlib.h>
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *start;
main()
{
void create();
void show();
int max_item();
start=NULL;
create();
printf("\nThe Linked List :\n");
show();
printf("\nThe largest element=%d",max_item());
getch();
}
void create()
{
node *ptr,*temp,*new_node;
int n,val,i;
printf("\nEnter the no. of nodes :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("\nOverflow");
return;
}
printf("\nEnter the value for node %d :",i);
scanf("%d",&val);
new_node->info=val;
new_node->next=NULL;
if(start==NULL)
{
ptr=new_node;
start=ptr;
}
else
{
ptr->next=new_node;
ptr=ptr->next;
}
}
return;
}
void show()
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->next==NULL)
printf("%d",ptr->info);
else
printf("%d-->",ptr->info);
ptr=ptr->next;
}
return;
}
int max_item()
{
node *ptr;
int max;
ptr=start;
max=ptr->info;
ptr=ptr->next;
while(ptr!=NULL)
{
if(ptr->info > max)
{
max=ptr->info;
}
ptr=ptr->next;
}
return(max);
}
No comments