Вы находитесь на странице: 1из 5

//IMPLEMENT PRIORITY QUEUE USING HEAPS #include<stdio.h> #include<conio.h> #include<stdlib.

h> struct minheap { int data[20],size; }; typedef struct minheap heap; heap *h; void insert(int,heap*); void delet(heap*); void display(heap*); int x; void main() { int choice; clrscr(); h->size=0; do { printf("\nCHOICES"); printf("\n*******"); printf("\n1.Insert an element"); printf("\n2.Delete an element"); printf("\n3.Display"); printf("\n4.Exit"); printf("\n enter the choice"); scanf("%d",&choice); switch(choice) { case 1:

{ do { printf("\n Enter the element to be inserted"); scanf("%d",&x); insert(x,h); }while(x!=0); break; } case 2: { delet(h); break; } case 3: { printf("\n The Min Heap Elements are"); display(h); break; } case 4: { exit(0); break; } default: { printf("\n Enter the right choice"); break; } } }while(choice!=4);

} void insert(int x,heap *h) { int i; for(i=++h->size;h->data[i/2]>x;i=i/2) h->data[i]=h->data[i/2]; h->data[i]=x; } void delet(heap *h) { int i,child,last; printf("\n Deleted element=%d",h->data[1]); last=h->data[h->size--]; for(i=1;2*i<=h->size;i=child) { child=2*i; if(h->data[2*i]>h->data[2*i+1]) child++; if(last>h->data[child]) h->data[i]=h->data[child]; else break; } h->data[i]=last; } void display(heap *h) { int i; for(i=0;i<=h->size;i++) printf("\nData[%d] = %d",i,h->data[i]); }

EXP NO: PROGRAM NAME:

OUTPUT: Choices: ******* 1.insert an element


2.delete an element 3.display 4. exit
enter the choice : 1 enter the element to be inserted enter the element to be inserted 8 9

enter the element to be inserted 2 enter the element to be inserted 1 enter the element to be inserted 0

Choices: ******* 1.insert an element


2.delete an element 3.display 4. exit
enter the choice : 2 deleted element 1

Choices: *******

1.insert an element
2.delete an element 3.display 4. exit
enter the choice : 3 the min heap elements are: data[0] = 0 data[1] = 2 data[2] = 8 data[3] = 9

Choices: ******* 1.insert an element


2.delete an element 3.display 4. exit
enter the choice : 4

Вам также может понравиться