0% found this document useful (0 votes)
3 views3 pages

Max Heap Program

The document contains a C program that implements a Max Heap data structure with functionalities for inserting, deleting, and printing elements. It includes functions for adjusting the heap, swapping elements, and creating the heap from user input. The program operates in a loop, allowing users to perform operations until they choose to exit.

Uploaded by

tocomo5256
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Max Heap Program

The document contains a C program that implements a Max Heap data structure with functionalities for inserting, deleting, and printing elements. It includes functions for adjusting the heap, swapping elements, and creating the heap from user input. The program operates in a loop, allowing users to perform operations until they choose to exit.

Uploaded by

tocomo5256
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

/* MAX HEAP */

#include<stdio.h>
#include<stdlib.h>
void max_adjust(int);
void swap(int*,int*);
void create();
void printlist();
int a[20],n;
int main()
{
int ch,x,i;
create();
printlist();
while(1)
{
printf("\n******\nMENU\n******\n");
printf("[Link]\[Link]\[Link]\[Link]\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: n++;
printf("\nEnter the element to be insert:");
scanf("%d",&a[n]);
for(i=n/2;i>=1;i--)
max_adjust(i);
printlist();
break;
case 2: swap(&a[1],&a[n]);
x=a[n];
n--;
max_adjust(1);
printf("\nDeleted number is:%d",x);
printlist();
break;

case 3: printlist();
break;
case 4: exit(0);
}
}
return 0;
}
void create()
{
int i;
printf("\nenter the size of array:");
scanf("%d",&n);
printf("\nenter the elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=n/2;i>=1;i--)
max_adjust(i);
}
void max_adjust(int i)
{
int child,temp;
for(temp=a[i];(2*i)<=n;i=child)
{
child=2*i;
if((child<n)&&(a[child+1]>a[child]))
child++;
if(temp<a[child])
a[i]=a[child];
else
break;
}
a[i]=temp;
}
void swap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
void printlist()
{
int i;
printf("\nElements in MAX-HEAP\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);

You might also like