C Program to Implement a Heap and Perform Heap Operations

«
»
This C Program implements a heap & provide insertion & deletion operation.

Here is source code of the C Program to implement a heap & provide insertion & deletion operation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement a Heap & provide Insertion & Deletion Operation 
  3.  */
  4. #include <stdio.h>
  5.  
  6. int array[100], n;
  7. main()
  8. {
  9.     int choice, num;
  10.     n = 0;/*Represents number of nodes in the heap*/
  11.     while(1)
  12.     {
  13.         printf("1.Insert the element \n");
  14.         printf("2.Delete the element \n");
  15.         printf("3.Display all elements \n");
  16.         printf("4.Quit \n");
  17.         printf("Enter your choice : ");
  18.         scanf("%d", &choice);
  19.         switch(choice)
  20.         {
  21.         case 1:
  22.             printf("Enter the element to be inserted to the list : ");
  23.             scanf("%d", &num);
  24.             insert(num, n);
  25.             n = n + 1;
  26.             break;
  27.         case 2:
  28.             printf("Enter the elements to be deleted from the list: ");
  29.             scanf("%d", &num);
  30.             delete(num);
  31.             break;
  32.         case 3:
  33.             display();
  34.             break;
  35.         case 4:
  36.             exit(0);
  37.         default:
  38.             printf("Invalid choice \n");
  39.     }/*End  of switch */
  40. }/*End of while */
  41. }/*End of main()*/
  42.  
  43. display()
  44. {
  45.     int i;
  46.     if (n == 0)
  47.     {
  48.         printf("Heap is empty \n");
  49.         return;
  50.     }
  51.     for (i = 0; i < n; i++)
  52.         printf("%d ", array[i]);
  53.     printf("\n");
  54. }/*End of display()*/
  55.  
  56. insert(int num, int location)
  57. {
  58.     int parentnode;
  59.     while (location > 0)
  60.     {
  61.         parentnode =(location - 1)/2;
  62.         if (num <= array[parentnode])
  63.         {
  64.             array[location] = num;
  65.             return;
  66.         }
  67.         array[location] = array[parentnode];
  68.         location = parentnode;
  69.     }/*End of while*/
  70.     array[0] = num; /*assign number to the root node */
  71. }/*End of insert()*/
  72.  
  73. delete(int num)
  74. {
  75.     int left, right, i, temp, parentnode;
  76.  
  77.     for (i = 0; i < num; i++) {
  78.         if (num == array[i])
  79.             break;
  80.     }
  81.     if (num != array[i])
  82.     {
  83.         printf("%d not found in heap list\n", num);
  84.         return;
  85.     }
  86.     array[i] = array[n - 1];
  87.     n = n - 1;
  88.     parentnode =(i - 1) / 2; /*find parentnode of node i */
  89.     if (array[i] > array[parentnode])
  90.     {
  91.         insert(array[i], i);
  92.         return;
  93.     }
  94.     left = 2 * i + 1; /*left child of i*/
  95.     right = 2 * i + 2; /* right child of i*/
  96.     while (right < n)
  97.     {
  98.         if (array[i] >= array[left] && array[i] >= array[right])
  99.             return;
  100.         if (array[right] <= array[left])
  101.         {
  102.             temp = array[i];
  103.             array[i] = array[left];
  104.             array[left] = temp;
  105.             i = left;
  106.         }
  107.         else
  108.         {
  109.             temp = array[i];
  110.             array[i] = array[right];
  111.             array[right] = temp;
  112.             i = right;
  113.         }
  114.         left = 2 * i + 1;
  115.         right = 2 * i + 2;
  116.     }/*End of while*/
  117.     if (left == n - 1 && array[i])    {
  118.         temp = array[i];
  119.         array[i] = array[left];
  120.         array[left] = temp;
  121.     }
  122. }

advertisement
$ cc pgm66.c
$ a.out
1.Insert the element
2.Delete the element
3.Display all elements
4.Quit
Enter your choice : 1
Enter the element to be inserted to the list : 30
1.Insert the element
2.Delete the element
3.Display all elements
4.Quit
Enter your choice : 1
Enter the element to be inserted to the list : 50
1.Insert the element
2.Delete the element
3.Display all elements
4.Quit
Enter your choice : 1
Enter the element to be inserted to the list : 70
1.Insert the element
2.Delete the element
3.Display all elements
4.Quit
Enter your choice : 2
Enter the elements to be deleted from the list: 10
10 not found in heap list
1.Insert the element
2.Delete the element
3.Display all elements
4.Quit
Enter your choice : 2
Enter the elements to be deleted from the list: 50
1.Insert the element
2.Delete the element
3.Display all elements
4.Quit
Enter your choice : 1
Enter the element to be inserted to the list : 100
1.Insert the element
2.Delete the element
3.Display all elements
4.Quit
Enter your choice : 3
100 30 70
1.Insert the element
2.Delete the element
3.Display all elements
4.Quit
Enter your choice : 4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Trees, go to Trees. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.