-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHeap.c
executable file
·74 lines (69 loc) · 1.65 KB
/
Heap.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
/*
* C Program to Implement a Heap & provide Insertion & Deletion Operation
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int array[100],n;
void insert(int,int);
void display();
int main()
{
int choice, num;
n = 0;/*Represents number of nodes in the heap*/
while(1)
{
printf("1.Insert the element \n");
printf("2.Display the element \n");
printf("3.Exit \n");
//printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted to the list : ");
scanf("%d", &num);
insert(num, n);
n = n + 1;
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf("Invalid choice \n");
}/*End of switch */
}/*End of while */
getch();
return 0;
}/*End of main()*/
void display()
{
int i;
if (n == 0)
{
printf("Heap is empty \n");
return;
}
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n");
}/*End of display()*/
void insert(int num, int location)
{
int parentnode;
while (location > 0)
{
parentnode =(location - 1)/2;
if (num <= array[parentnode])
{
array[location] = num;
return;//exits
}
array[location] = array[parentnode];
location = parentnode;
}/*End of while*/
array[0] = num; /*assign number to the root node */
}/*End of insert()*/