import
java.util.*;
class
GFG{
static
HashSet<Integer> hashmap =
new
HashSet<Integer>();
static
class
Node {
int
data;
Node next;
};
static
Node push(Node head_ref,
int
new_data)
{
Node new_node =
new
Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return
head_ref;
}
static
Node deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;
if
(head_ref ==
null
|| del ==
null
)
return
null
;
if
(head_ref == del)
head_ref = del.next;
while
(temp.next != del) {
temp = temp.next;
}
temp.next = del.next;
del =
null
;
return
head_ref;
}
static
int
largestElement(Node head_ref)
{
int
max = Integer.MIN_VALUE;
Node head = head_ref;
while
(head !=
null
) {
if
(max < head.data)
max = head.data;
head = head.next;
}
return
max;
}
static
void
createHash(
int
maxElement)
{
int
prev =
0
, curr =
1
;
hashmap.add(prev);
hashmap.add(curr);
while
(curr <= maxElement) {
int
temp = curr + prev;
hashmap.add(temp);
prev = curr;
curr = temp;
}
}
static
int
printFibonacci(Node head_ref)
{
int
count =
0
;
Node ptr = head_ref;
System.out.print(
"Fibonacci nodes = "
);
while
(ptr !=
null
) {
if
(hashmap.contains(ptr.data)) {
System.out.print(ptr.data+
" "
);
}
ptr = ptr.next;
}
System.out.println();
return
0
;
}
static
int
countFibonacci(Node head_ref)
{
int
count =
0
;
Node ptr = head_ref;
while
(ptr !=
null
) {
if
(hashmap.contains(ptr.data)) {
count++;
}
ptr = ptr.next;
}
return
count;
}
static
void
minmaxFibonacciNodes(Node head_ref)
{
int
maxEle = largestElement(head_ref);
int
minimum = Integer.MAX_VALUE;
int
maximum = Integer.MIN_VALUE;
Node ptr = head_ref;
while
(ptr !=
null
) {
if
(hashmap.contains(ptr.data)) {
minimum
= Math.min(minimum, ptr.data);
maximum
= Math.max(maximum, ptr.data);
}
ptr = ptr.next;
}
System.out.print(
"Minimum Fibonacci node: "
+ minimum +
"\n"
);
System.out.print(
"Maximum Fibonacci node: "
+ maximum +
"\n"
);
}
static
Node deleteFibonacciNodes(Node head_ref)
{
Node ptr = head_ref;
Node next;
while
(ptr !=
null
) {
next = ptr.next;
if
(hashmap.contains(ptr.data))
deleteNode(head_ref, ptr);
ptr = next;
}
return
head_ref;
}
static
void
printList(Node head)
{
while
(head !=
null
) {
System.out.print(head.data+
" "
);
head = head.next;
}
}
static
void
operations(Node head)
{
int
maxEle = largestElement(head);
createHash(maxEle);
printFibonacci(head);
System.out.print(
"Count of Fibonacci nodes = "
+ countFibonacci(head) +
"\n"
);
minmaxFibonacciNodes(head);
head = deleteFibonacciNodes(head);
System.out.print(
"List after deletion: "
);
printList(head);
}
public
static
void
main(String[] args)
{
Node head =
null
;
head = push(head,
13
);
head = push(head,
6
);
head = push(head,
8
);
head = push(head,
16
);
head = push(head,
15
);
operations(head);
}
}