-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BitonicPoint.c
155 lines (121 loc) · 2.65 KB
/
BitonicPoint.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Code Description:
Through this C program ,we find the Bitonic Point in a Linked List.
Bitonic Point is a point in bitonic sequence( Sequence of numbers which is first strictly increasing then after a point strictly decreasing) before
which elements are strictly increasing and after which elements are strictly decreasing.
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/* Defining a Node */
struct Node {
int data;
struct Node* next;
};
/* Header points to first node and Last points to last one */
struct Node *header,*last;
int Bitonic_Point()
{
struct Node *t1 = header;
/* If list is empty or contains one Node */
if(header == NULL || header->next == NULL)
{
return -1;
}
/* Not in Bitonic sequence */
if (t1->data > t1->next->data)
return -1;
while(t1->next != NULL)
{
/* If the data in current Node is greater than Next Node */
if(t1->data > t1->next->data)
{
break;
}
/* Otherwise Traversal continues */
t1=t1->next;
}
/* Storing data (Bitonic Point )of the Current Node */
int val = t1->data;
/* Updating to its Next Node */
t1=t1->next;
while(t1->next != NULL)
{
/* If data in current Node is less than Next Node's data */
if(t1->data < t1->next->data)
{
/* Bitnoic Point doesn't exist */
return -1;
}
t1=t1->next;
}
return val;
}
/* Function to create a Linked List */
void create()
{
struct Node *temp=(struct Node*)malloc(sizeof(struct Node));
printf("\nEnter value of node : ");
scanf("%d",&temp->data);
temp->next=NULL;
if (header==NULL)
{
header=temp;
last=temp;
return;
}
else
{
last->next=temp;
last=temp;
return;
}
}
/* Function to print nodes in a given linked list */
void show()
{
struct Node *temp=header;
while(temp!=NULL)
{
printf("-->%d",temp->data);
temp=temp->next;
}
printf("\n");
}
/* Driver Function */
void main()
{
int i,num,B_point;
printf("Enter the number of nodes: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
create();
printf("\nLinked List is : \n");
show();
printf("\nBitonic Point in the Linked List :- \n");
B_point=Bitonic_Point();
if(B_point == -1)
{
printf("\nNo Bitonic Point exists\n");
return;
}
printf("\nValue of Bitonic Point is %d",B_point);
}
/*
COMPLEXITY:
Time Complexity:O(n)
Space Complexity:O(n)
OUTPUT:
Enter the number of nodes: 7
Enter value of node : 1
Enter value of node : 2
Enter value of node : 3
Enter value of node : 4
Enter value of node : 3
Enter value of node : 2
Enter value of node : 1
Linked List is :
-->1-->2-->3-->4-->3-->2-->1
Bitonic Point in the Linked List :-
Value of Bitonic Point is 4
*/