Please note the heap should be implemented using linked list.
Program Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*Defining the HEAP Node structer*/
typedef struct heap_node{
int data;
struct heap_node* parent;
struct heap_node* left_child;
struct heap_node* right_child;
struct heap_node* next_node;
struct heap_node* prev_node;
} heap_node;
/*Declaration of functions*/
void insert_into_pqueue(heap_node** start, heap_node** end, heap_node** prev, heap_node** comp);
int dequeue_from_pqueue(heap_node** start, heap_node** end, heap_node** prev, heap_node** comp);
void display_priority_queue(heap_node* start);
void heapify_down(heap_node** comp);
void heapify_up(heap_node** start, heap_node** comp);
/*Starting of the Main Function*/
int main()
{
heap_node* start = NULL;
heap_node* end = NULL;
heap_node* prev = NULL;
heap_node* comp = NULL;
int choice, value;
/*Loop to receive instructions from the user*/
while(1)
{
printf("\nPlease enter a choice:\n**********************\n");
printf("1) Insert element into Priority Queue\n2) Dequeue from Priority Queue\n3) Display Elements in Priority Queue\n4) Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_into_pqueue(&start, &end, &prev, &comp);
heapify_down(&comp);
printf("Priority Queue after Enqueue:\n*****************************\n");
display_priority_queue(start);
break;
case 2:
value = dequeue_from_pqueue(&start, &end, &prev, &comp);
if (value == -1)
{ printf("\nNo Element found in Queue...\n\n"); }
else
{ printf("\nElement is - %d\n\n", value); }
heapify_up(&start, &comp);
printf("Priority Queue after Dequeue:\n*****************************\n");
display_priority_queue(start);
break;
case 3:
printf("\nPriority Queue:\n****************\n");
display_priority_queue(start);
break;
case 4:
return 0;
break;
}
}
return 0;
}
/*End of the main Function*/
/*Function definition to insert the node into the heap*/
void insert_into_pqueue(heap_node** start, heap_node** end, heap_node** prev, heap_node** comp)
{
heap_node* temp_node = NULL;
temp_node = (heap_node*)malloc(sizeof(heap_node));
if (temp_node == NULL)
{
printf("Memory for Heap Node can't be allocated");
exit(0);
}
printf("Enter the value of the heap node: ");
scanf("%d",&temp_node->data);
/*If there is no elements in the Heap */
if(*end == NULL)
{
*start = temp_node;
*end = temp_node;
*prev = temp_node;
*comp = temp_node;
/*Init the pointers with NULL Values*/
temp_node->parent = NULL;
temp_node->left_child = NULL;
temp_node->right_child = NULL;
temp_node->next_node = NULL;
temp_node->prev_node = NULL;
}
else /*If the Heap is already having nodes it it*/
{
if((*end)->left_child == NULL)
{
(*end)->left_child = temp_node;
/*Manupulating the pointers values*/
temp_node->parent = *end;
temp_node->left_child = NULL;
temp_node->right_child = NULL;
temp_node->next_node = NULL;
temp_node->prev_node = *prev;
(*prev)->next_node = temp_node;
*prev = temp_node;
*comp = temp_node;
}
else
{
(*end)->right_child = temp_node;
/*Manupulating the pointers values*/
temp_node->parent = *end;
temp_node->left_child = NULL;
temp_node->right_child = NULL;
temp_node->next_node = NULL;
temp_node->prev_node = *prev;
(*prev)->next_node = temp_node;
*prev = temp_node;
*comp = temp_node;
*end = (*end)->next_node;
}
}
}
/*Function definition of delete from Priority Queue*/
int dequeue_from_pqueue(heap_node** start, heap_node** end, heap_node** prev, heap_node** comp)
{
int element = -1;
if(*start == NULL)
{
return element;
}
element = (*start)->data;
if((*start)->left_child == NULL)
{
free(*start);
*start = NULL;
*end = NULL;
*prev = NULL;
*comp = NULL;
}
else
{
if((*end)->left_child == NULL)
{
(*start)->data = (*prev)->data;
*end = (*end)->prev_node;
(*end)->right_child = NULL;
*comp = *prev;
*prev = (*prev)->prev_node;
(*prev)->next_node = NULL;
free(*comp);
*comp = *prev;
}
else
{
(*start)->data = (*prev)->data;
(*end)->left_child = NULL;
*comp = *prev;
*prev = (*prev)->prev_node;
(*prev)->next_node = NULL;
free(*comp);
*comp = *prev;
}
}
return element;
}
/*Function definition to display the Priority Queue*/
void display_priority_queue(heap_node* start)
{
if(start == NULL)
{
printf("Queue is empty...\n\n");
return;
}
heap_node* temp_start = start;
while(1)
{
if(temp_start == NULL)break;
printf("%d ", temp_start->data);
temp_start = temp_start->next_node;
}
printf("\n\n");
}
/*Function defination to heapify the heap from down*/
void heapify_down(heap_node** comp)
{
int i;
while((*comp)->parent != NULL)
{
heap_node* temp_parent = (*comp)->parent;
if( (*comp)->data < temp_parent->data )
{
i = (*comp)->data;
(*comp)->data = temp_parent->data;
*comp = (*comp)->parent;
(*comp)->data = i;
}
else
{ break; }
}
}
/*Function definition to heapify the heap from down*/
void heapify_up(heap_node** start, heap_node** comp)
{
int i;
*comp = *start;
/*If No node is there in Heap*/
if(*start == NULL)
{ return; }
/*If just in one node is there in the Heap*/
if((*comp)->left_child == NULL && (*comp)->right_child == NULL)
{ return; }
/*If only two nodes are there in the Heap*/
if((*comp)->right_child == NULL)
{
heap_node* temp_child = (*comp)->left_child;
if((*comp)->data > temp_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_child->data;
*comp = (*comp)->left_child;
(*comp)->data = i;
}
return;
}
/*If more than two nodes are there in the Heap*/
while((*comp)->left_child != NULL || (*comp)->right_child != NULL )
{
if((*comp)->right_child == NULL)
{
heap_node* temp_left_child = (*comp)->left_child;
if((*comp)->data >= temp_left_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_left_child->data;
*comp = (*comp)->left_child;
(*comp)->data = i;
}
return;
}
else
{
heap_node* temp_left_child = (*comp)->left_child;
heap_node* temp_right_child = (*comp)->right_child;
if(temp_left_child->data == temp_right_child->data)
{
if((*comp)->data <= temp_left_child->data)
{
return;
}
if((*comp)->data > temp_left_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_left_child->data;
*comp = (*comp)->left_child;
(*comp)->data = i;
continue;
}
}
else if(temp_left_child->data < temp_right_child->data)
{
if((*comp)->data <= temp_left_child->data)
{
return;
}
else if((*comp)->data > temp_left_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_left_child->data;
*comp = (*comp)->left_child;
(*comp)->data = i;
continue;
}
}
else
{
if((*comp)->data <= temp_right_child->data)
{
return;
}
if((*comp)->data > temp_right_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_right_child->data;
*comp = (*comp)->right_child;
(*comp)->data = i;
continue;
}
}
}
}
}
/********** End of the Program **********/
Program Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*Defining the HEAP Node structer*/
typedef struct heap_node{
int data;
struct heap_node* parent;
struct heap_node* left_child;
struct heap_node* right_child;
struct heap_node* next_node;
struct heap_node* prev_node;
} heap_node;
/*Declaration of functions*/
void insert_into_pqueue(heap_node** start, heap_node** end, heap_node** prev, heap_node** comp);
int dequeue_from_pqueue(heap_node** start, heap_node** end, heap_node** prev, heap_node** comp);
void display_priority_queue(heap_node* start);
void heapify_down(heap_node** comp);
void heapify_up(heap_node** start, heap_node** comp);
/*Starting of the Main Function*/
int main()
{
heap_node* start = NULL;
heap_node* end = NULL;
heap_node* prev = NULL;
heap_node* comp = NULL;
int choice, value;
/*Loop to receive instructions from the user*/
while(1)
{
printf("\nPlease enter a choice:\n**********************\n");
printf("1) Insert element into Priority Queue\n2) Dequeue from Priority Queue\n3) Display Elements in Priority Queue\n4) Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_into_pqueue(&start, &end, &prev, &comp);
heapify_down(&comp);
printf("Priority Queue after Enqueue:\n*****************************\n");
display_priority_queue(start);
break;
case 2:
value = dequeue_from_pqueue(&start, &end, &prev, &comp);
if (value == -1)
{ printf("\nNo Element found in Queue...\n\n"); }
else
{ printf("\nElement is - %d\n\n", value); }
heapify_up(&start, &comp);
printf("Priority Queue after Dequeue:\n*****************************\n");
display_priority_queue(start);
break;
case 3:
printf("\nPriority Queue:\n****************\n");
display_priority_queue(start);
break;
case 4:
return 0;
break;
}
}
return 0;
}
/*End of the main Function*/
/*Function definition to insert the node into the heap*/
void insert_into_pqueue(heap_node** start, heap_node** end, heap_node** prev, heap_node** comp)
{
heap_node* temp_node = NULL;
temp_node = (heap_node*)malloc(sizeof(heap_node));
if (temp_node == NULL)
{
printf("Memory for Heap Node can't be allocated");
exit(0);
}
printf("Enter the value of the heap node: ");
scanf("%d",&temp_node->data);
/*If there is no elements in the Heap */
if(*end == NULL)
{
*start = temp_node;
*end = temp_node;
*prev = temp_node;
*comp = temp_node;
/*Init the pointers with NULL Values*/
temp_node->parent = NULL;
temp_node->left_child = NULL;
temp_node->right_child = NULL;
temp_node->next_node = NULL;
temp_node->prev_node = NULL;
}
else /*If the Heap is already having nodes it it*/
{
if((*end)->left_child == NULL)
{
(*end)->left_child = temp_node;
/*Manupulating the pointers values*/
temp_node->parent = *end;
temp_node->left_child = NULL;
temp_node->right_child = NULL;
temp_node->next_node = NULL;
temp_node->prev_node = *prev;
(*prev)->next_node = temp_node;
*prev = temp_node;
*comp = temp_node;
}
else
{
(*end)->right_child = temp_node;
/*Manupulating the pointers values*/
temp_node->parent = *end;
temp_node->left_child = NULL;
temp_node->right_child = NULL;
temp_node->next_node = NULL;
temp_node->prev_node = *prev;
(*prev)->next_node = temp_node;
*prev = temp_node;
*comp = temp_node;
*end = (*end)->next_node;
}
}
}
/*Function definition of delete from Priority Queue*/
int dequeue_from_pqueue(heap_node** start, heap_node** end, heap_node** prev, heap_node** comp)
{
int element = -1;
if(*start == NULL)
{
return element;
}
element = (*start)->data;
if((*start)->left_child == NULL)
{
free(*start);
*start = NULL;
*end = NULL;
*prev = NULL;
*comp = NULL;
}
else
{
if((*end)->left_child == NULL)
{
(*start)->data = (*prev)->data;
*end = (*end)->prev_node;
(*end)->right_child = NULL;
*comp = *prev;
*prev = (*prev)->prev_node;
(*prev)->next_node = NULL;
free(*comp);
*comp = *prev;
}
else
{
(*start)->data = (*prev)->data;
(*end)->left_child = NULL;
*comp = *prev;
*prev = (*prev)->prev_node;
(*prev)->next_node = NULL;
free(*comp);
*comp = *prev;
}
}
return element;
}
/*Function definition to display the Priority Queue*/
void display_priority_queue(heap_node* start)
{
if(start == NULL)
{
printf("Queue is empty...\n\n");
return;
}
heap_node* temp_start = start;
while(1)
{
if(temp_start == NULL)break;
printf("%d ", temp_start->data);
temp_start = temp_start->next_node;
}
printf("\n\n");
}
/*Function defination to heapify the heap from down*/
void heapify_down(heap_node** comp)
{
int i;
while((*comp)->parent != NULL)
{
heap_node* temp_parent = (*comp)->parent;
if( (*comp)->data < temp_parent->data )
{
i = (*comp)->data;
(*comp)->data = temp_parent->data;
*comp = (*comp)->parent;
(*comp)->data = i;
}
else
{ break; }
}
}
/*Function definition to heapify the heap from down*/
void heapify_up(heap_node** start, heap_node** comp)
{
int i;
*comp = *start;
/*If No node is there in Heap*/
if(*start == NULL)
{ return; }
/*If just in one node is there in the Heap*/
if((*comp)->left_child == NULL && (*comp)->right_child == NULL)
{ return; }
/*If only two nodes are there in the Heap*/
if((*comp)->right_child == NULL)
{
heap_node* temp_child = (*comp)->left_child;
if((*comp)->data > temp_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_child->data;
*comp = (*comp)->left_child;
(*comp)->data = i;
}
return;
}
/*If more than two nodes are there in the Heap*/
while((*comp)->left_child != NULL || (*comp)->right_child != NULL )
{
if((*comp)->right_child == NULL)
{
heap_node* temp_left_child = (*comp)->left_child;
if((*comp)->data >= temp_left_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_left_child->data;
*comp = (*comp)->left_child;
(*comp)->data = i;
}
return;
}
else
{
heap_node* temp_left_child = (*comp)->left_child;
heap_node* temp_right_child = (*comp)->right_child;
if(temp_left_child->data == temp_right_child->data)
{
if((*comp)->data <= temp_left_child->data)
{
return;
}
if((*comp)->data > temp_left_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_left_child->data;
*comp = (*comp)->left_child;
(*comp)->data = i;
continue;
}
}
else if(temp_left_child->data < temp_right_child->data)
{
if((*comp)->data <= temp_left_child->data)
{
return;
}
else if((*comp)->data > temp_left_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_left_child->data;
*comp = (*comp)->left_child;
(*comp)->data = i;
continue;
}
}
else
{
if((*comp)->data <= temp_right_child->data)
{
return;
}
if((*comp)->data > temp_right_child->data)
{
i = (*comp)->data;
(*comp)->data = temp_right_child->data;
*comp = (*comp)->right_child;
(*comp)->data = i;
continue;
}
}
}
}
}
/********** End of the Program **********/
No comments:
Post a Comment