Advertisement
steverobinson

Steve Robinson

Aug 26th, 2010
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 KB | None | 0 0
  1. //Program to Implement struct binaryheap Queue using Min Heap
  2. #include <stdio.h>
  3. #include <conio.h>
  4.  
  5. struct binaryheap
  6. {
  7.     int maxsize;
  8.     int size;
  9.     int *elements;
  10. }h;
  11.  
  12. void display()
  13. {
  14.     int i;
  15.     printf("\nThe Elements are: \n");
  16.     for(i=1;i<=h.size;i++)
  17.     {
  18.         printf("\n%d",h.elements[i]);
  19.     }
  20. }
  21.  
  22. void insert (int x)
  23. {
  24.     int i,temp;
  25.     if(h.size==0)
  26.         {
  27.             h.size++;
  28.             h.elements[1]=x;
  29.         }
  30.     else
  31.     {
  32.         h.size++;
  33.         h.elements[h.size]=x;
  34.         for(i=h.size;h.elements[i]<h.elements[i/2]&&i/2!=0;i=i/2)
  35.         {
  36.             temp=h.elements[i];
  37.             h.elements[i]=h.elements[i/2];
  38.             h.elements[i/2]=temp;
  39.         }
  40.     }
  41. }
  42.  
  43.  
  44. int  main()
  45. {
  46.     int temp,ch,i;
  47.     h.size=0;
  48.     printf("\nEnter Maximum Number of Elements in the Queue: ");
  49.     scanf("%d",&h.maxsize);
  50.     h.elements=(int *)malloc((h.maxsize+1)*sizeof(int));
  51.     menu:
  52.     printf("\nMenu:\n1. Insert Elements\n2. Display Heap\n3. Exit\nEnter Choice.....");
  53.     scanf("%d",&ch);
  54.  
  55.     switch(ch)
  56.     {
  57.         case 1:
  58.             do{
  59.                 if(h.size<h.maxsize)
  60.                 {
  61.                     printf("\nEnter the element: ");
  62.                     scanf("%d",&temp);
  63.                     insert(temp);
  64.                 }
  65.                 else
  66.                 {
  67.                     printf("\nQueue Full!\n");
  68.                     goto menu;
  69.                 }
  70.                 printf("\nInsert More ? Yes (1) No (0)");
  71.                 scanf("%d",&ch);
  72.             }while(ch);
  73.             break;
  74.      
  75.       case 2:
  76.         display();
  77.         break;
  78.  
  79.       case 3:
  80.         printf("\nThank You!");
  81.         getch();
  82.         return 0;
  83.  
  84.       default:
  85.         printf("\nInvalid choice!\n");
  86.         goto menu;
  87.    
  88.     }
  89.    
  90.     goto menu;
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement