Skip to content

Commit 1524d6e

Browse files
fixed #163 added program to find max element in ll
1 parent 3e0bcb2 commit 1524d6e

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node
5+
{
6+
int data;
7+
struct Node *next;
8+
} *first = NULL;
9+
10+
void Create(int A[], int n)
11+
{
12+
int i;
13+
struct Node *t, *last;
14+
first = (struct Node *)malloc(sizeof(struct Node));
15+
first->data = A[0];
16+
first->next = NULL;
17+
last = first;
18+
19+
for (i = 1; i < n; i++)
20+
{
21+
t = (struct Node *)malloc(sizeof(struct Node));
22+
t->data = A[i];
23+
t->next = NULL;
24+
last->next = t;
25+
last = t;
26+
}
27+
}
28+
29+
int Max(struct Node *p)
30+
{
31+
int max = INT_MIN;
32+
while (p != NULL)
33+
{
34+
if (p->data > max)
35+
max = p->data;
36+
p = p->next;
37+
}
38+
return max;
39+
}
40+
41+
int Min(struct Node *p)
42+
{
43+
int min = INT_MAX;
44+
while (p != NULL)
45+
{
46+
if (p->data < min)
47+
min = p->data;
48+
p = p->next;
49+
}
50+
return min;
51+
}
52+
53+
int RMax(struct Node *p)
54+
{
55+
int x = 0;
56+
if (p == 0)
57+
return INT_MIN;
58+
x = RMax(p->next);
59+
if (x > p->data)
60+
return x;
61+
else
62+
return p->data;
63+
}
64+
int main()
65+
{
66+
int A[] = {3, 5, 7, 10, 15, 8, 12, 2};
67+
Create(A, 8);
68+
printf("Maximum Number is : %d \n", RMax(first));
69+
printf("Minimum Number is : %d ", Min(first));
70+
71+
return 0;
72+
}
73+
74+
// Output :
75+
// Maximum Number is : 15
76+
// Minimum Number is : 2
77+
78+
// Iterative
79+
// Time : O(n), Space O(1)
80+
81+
// Recursive
82+
// Time : O(n), Space(n)

0 commit comments

Comments
 (0)