-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq10.c
58 lines (52 loc) · 1.2 KB
/
q10.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
/*
/*
/* Design, develop, and execute a program in C to create a max heap
/* of integers by accepting one element at a time and by inserting it
/* immediately in to the heap. Use the array representation for the
/* heap. Display the array at the end of insertion phase.
/*
*/
#include <stdio.h>
#define MAX 100
void insertHeap(int heap[], int *curr, int key) {
heap[++(*curr)] = key;
if(*curr == 0)
return;
int parent = (*curr - 1) / 2, child = *curr;
while(child != 0) {
if(heap[parent] < heap[child])
heap[parent] = heap[parent] + heap[child] - (heap[child] = heap[parent]);
child = parent;
parent = (child - 1) / 2;
}
}
void displayHeap(int heap[], int curr) {
int i = 0;
for(; i <= curr; i++)
printf(" %d", heap[i]);
printf("\n");
}
void main() {
int heap[MAX], curr = -1, item;
char choice;
printf("\n Enter");
printf("\n i to Insert");
printf("\n d to Display");
do {
printf("\n >> ");
scanf("%c", &choice);
switch(choice) {
case 'i':
printf(" Enter element to insert: ");
scanf(" %d", &item);
insertHeap(heap, &curr, item);
case 'd':
printf("\n");
displayHeap(heap, curr);
break;
default:
return;
}
scanf("%c", &choice);
} while(1);
}