Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int array[100], n;
- void insert(int num, int location);
- void hapus(int num);
- void display();
- int main() {
- int choice, num, root = 0;
- int quit = 0;
- n = 0;
- while(!quit) {
- printf("1.Insert the element \n");
- printf("2.Delete the element \n");
- printf("3.Display all elements \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:
- hapus(array[0]);
- break;
- case 3:
- display();
- break;
- case 4:
- quit = 1;
- break;
- default:
- printf("Invalid choice \n");
- }
- }
- }
- 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");
- }
- void insert(int num, int location) {
- int parentnode;
- while (location > 0) {
- parentnode = (location - 1)/2;
- if (num <= array[parentnode]) {
- array[location] = num;
- return;
- }
- array[location] = array[parentnode];
- location = parentnode;
- }
- array[0] = num;
- }
- void hapus(int num) {
- int left, right, i, temp, parentnode;
- for (i = 0; i < num; i++) {
- if (num == array[i])
- break;
- }
- if (num != array[i]) {
- printf("%d not found in heap list\n", num);
- return;
- }
- array[i] = array[n - 1];
- n = n - 1;
- parentnode =(i - 1) / 2;
- if (array[i] > array[parentnode]) {
- insert(array[i], i);
- return;
- }
- left = 2 * i + 1;
- right = 2 * i + 2;
- while (right < n) {
- if (array[i] >= array[left] && array[i] >= array[right])
- return;
- if (array[right] <= array[left]) {
- temp = array[i];
- array[i] = array[left];
- array[left] = temp;
- i = left;
- } else {
- temp = array[i];
- array[i] = array[right];
- array[right] = temp;
- i = right;
- }
- left = 2 * i + 1;
- right = 2 * i + 2;
- }
- if (left == n - 1 && array[i]) {
- temp = array[i];
- array[i] = array[left];
- array[left] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement