#include
using
namespace
std;
struct
Node {
int
data;
Node *prev, *next;
};
void
push(Node** head_ref,
int
new_data)
{
Node* new_node
= (Node*)
malloc
(
sizeof
(
struct
Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if
((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int
LargestInDLL(
struct
Node** head_ref)
{
struct
Node *max, *temp;
temp = max = *head_ref;
while
(temp != NULL) {
if
(temp->data > max->data)
max = temp;
temp = temp->next;
}
return
max->data;
}
void
createHash(set& hash,
int
maxElement)
{
int
prev = 0, curr = 1;
hash.insert(prev);
hash.insert(curr);
while
(curr pointer to head node pointer.
void
deleteNode(Node** head_ref, Node* del)
{
if
(*head_ref == NULL || del == NULL)
return
;
if
(*head_ref == del)
*head_ref = del->next;
if
(del->next != NULL)
del->next->prev = del->prev;
if
(del->prev != NULL)
del->prev->next = del->next;
free
(del);
return
;
}
void
deleteFibonacciNodes(Node** head_ref)
{
int
maxEle = LargestInDLL(head_ref);
set hash;
createHash(hash, maxEle);
Node* ptr = *head_ref;
Node* next;
while
(ptr != NULL) {
next = ptr->next;
if
(hash.find(ptr->data) != hash.end())
deleteNode(head_ref, ptr);
ptr = next;
}
}
void
printList(Node* head)
{
while
(head != NULL) {
cout <data <next;
}
}
int
main()
{
Node* head = NULL;
push(&head, 13);
push(&head, 6);
push(&head, 8);
push(&head, 16);
push(&head, 15);
cout <<
"Original List: "
;
printList(head);
deleteFibonacciNodes(&head);
cout <<
"\nModified List: "
;
printList(head);
}