-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq1test.c
46 lines (41 loc) · 845 Bytes
/
q1test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Find largest element in a linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert(struct node **head, int data)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->next = *head;
*head = temp;
}
int largest(struct node *head)
{
int max = head->data;
while (head != NULL)
{
if (head->data > max)
max = head->data;
head = head->next;
}
return max;
}
int main()
{
struct node *head = NULL;
int n, i, data;
printf("Enter the number of nodes: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the data: ");
scanf("%d", &data);
insert(&head, data);
}
printf("Largest element is %d", largest(head));
return 0;
}