Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program to Implement struct binaryheap Queue using Min Heap
- #include <stdio.h>
- #include <conio.h>
- struct binaryheap
- {
- int maxsize;
- int size;
- int *elements;
- }h;
- void display()
- {
- int i;
- printf("\nThe Elements are: \n");
- for(i=1;i<=h.size;i++)
- {
- printf("\n%d",h.elements[i]);
- }
- }
- void insert (int x)
- {
- int i,temp;
- if(h.size==0)
- {
- h.size++;
- h.elements[1]=x;
- }
- else
- {
- h.size++;
- h.elements[h.size]=x;
- for(i=h.size;h.elements[i]<h.elements[i/2]&&i/2!=0;i=i/2)
- {
- temp=h.elements[i];
- h.elements[i]=h.elements[i/2];
- h.elements[i/2]=temp;
- }
- }
- }
- int main()
- {
- int temp,ch,i;
- h.size=0;
- printf("\nEnter Maximum Number of Elements in the Queue: ");
- scanf("%d",&h.maxsize);
- h.elements=(int *)malloc((h.maxsize+1)*sizeof(int));
- menu:
- printf("\nMenu:\n1. Insert Elements\n2. Display Heap\n3. Exit\nEnter Choice.....");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- do{
- if(h.size<h.maxsize)
- {
- printf("\nEnter the element: ");
- scanf("%d",&temp);
- insert(temp);
- }
- else
- {
- printf("\nQueue Full!\n");
- goto menu;
- }
- printf("\nInsert More ? Yes (1) No (0)");
- scanf("%d",&ch);
- }while(ch);
- break;
- case 2:
- display();
- break;
- case 3:
- printf("\nThank You!");
- getch();
- return 0;
- default:
- printf("\nInvalid choice!\n");
- goto menu;
- }
- goto menu;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement