C Program to Implement a Heap & provide Insertion & Deletion Operation

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. }


$ 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.

Here’s the list of Best Reference 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.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Follow Manish & Sanfoundry