-
Notifications
You must be signed in to change notification settings - Fork 1
/
Maximum_and_minimum_element_in_a_linked_list.c
119 lines (119 loc) · 2.23 KB
/
Maximum_and_minimum_element_in_a_linked_list.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node
{
int data;
struct node *next; /*self-referential pointer*/
} *first = NULL;
void create(int A[], int n)
{
first = (struct node *)malloc(sizeof(struct node));
(*(first)).data = A[0];
(*(first)).next = NULL;
struct node *t, *last;
last = first;
for (int i = 1; i < n; i++)
{
t = (struct node *)malloc(sizeof(struct node));
(*(t)).data = A[i];
(*(t)).next = NULL;
(*(last)).next = t;
last = t;
}
}
void display(struct node *p)
{
while (p != NULL)
{
printf("%d\t", (*(p)).data);
p = (*(p)).next;
}
}
int max(struct node *p)
{
int max = (*(p)).data;
p = (*(p)).next;
while (p != NULL)
{
if ((*(p)).data > max)
{
max = (*(p)).data;
p = (*(p)).next;
}
else
{
p = (*(p)).next;
}
}
return max;
}
int min(struct node *p)
{
int min = (*(p)).data;
p = (*(p)).next;
while (p != NULL)
{
if ((*(p)).data < min)
{
min = (*(p)).data;
p = (*(p)).next;
}
else
{
p = (*(p)).next;
}
}
return min;
}
int rmax(struct node *p)
{
int x;
if (p == NULL)
{
return INT_MIN;
}
else
{
x = rmax((*(p)).next);
return x < (*(p)).data ? (*(p)).data : x;
/*if (x < (*(p)).data)
{
x = (*(p)).data;
return x;
}
else
{
return x;
}*/
}
}
int rmin(struct node *p)
{
int x;
if (p == NULL)
{
return INT_MAX;
}
else
{
x = rmin((*(p)).next);
return x > (*(p)).data ? (*(p)).data : x;
/*if (x > (*(p)).data)
{
x = (*(p)).data;
return x;
}
else
{
return x;
}*/
}
}
int main()
{
int A[10] = {2, 34, 1, 0, 789, 34567, 9, 98, -76, 0};
create(A, 10);
display(first);
printf("\nmaximum=%d and minimum=%d\n", rmax(first), rmin(first));
}