Question

In: Computer Science

Write a C program to implement the priority queue with the operations insert and extractmax. Sample...

Write a C program to implement the priority queue with the operations insert and extractmax.

Sample :

====Menu====

  1. insert
  2. extractmax
  3. display
  4. exit

Enter your choice: 1

Input a number: 2

enter any key to go to main menu

====Menu====

  1. insert
  2. extractmax
  3. display
  4. exit

Enter your choice: 1

Input a number: 4

enter any key to go to main menu

====Menu====

  1. insert
  2. extractmax
  3. display
  4. exit

Enter your choice: 1

Input a number: 6

enter any key to go to main menu

====Menu====

  1. insert
  2. extractmax
  3. display
  4. exit

Enter your choice: 2

enter any key to go to main menu

====Menu====

  1. insert
  2. extractmax
  3. display
  4. exit

Enter your choice: 3

Elements are : 2 4

enter any key to go to main menu

====Menu====

  1. insert
  2. extractmax
  3. display
  4. exit

Enter your choice: 4

bye

COMMENT COMPLETE CODE WITH EXPLANATION PLEASE. THANKS

Solutions

Expert Solution

// Priority Queue implementation in C

#include <stdio.h>
int size = 0;//global variable size
void swap(int *a, int *b) {//to swap elements
int temp = *b;
*b = *a;
*a = temp;
}

// Function to heapify the tree
void heapify(int array[], int size, int i) {
if (size == 1) {
    printf("Single element in the heap");
} else {
    // Find the largest among root, left child and right child
    int largest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;
    if (l < size && array[l] > array[largest])
      largest = l;
    if (r < size && array[r] > array[largest])
      largest = r;

    // Swap and continue heapifying if root is not largest
    if (largest != i) {
      swap(&array[i], &array[largest]);
      heapify(array, size, largest);
    }
}
}

// Function to insert an element into the tree
void insert(int array[], int newNum) {
if (size == 0) {
    array[0] = newNum;
    size += 1;
} else {
    array[size] = newNum;
    size += 1;
    for (int i = size / 2 - 1; i >= 0; i--) {
      heapify(array, size, i);
    }
}
}

// Function to delete an element from the tree
void deleteRoot(int array[], int num) {
int i;
for (i = 0; i < size; i++) {
    if (num == array[i])
      break;
}

swap(&array[i], &array[size - 1]);
size -= 1;
for (int i = size / 2 - 1; i >= 0; i--) {
    heapify(array, size, i);
}
}

// Print the array
void printArray(int array[], int size) {
for (int i = size-1; i >=0; --i)
    printf("%d ", array[i]);
printf("\n");
}

// Driver code
int main() {
int array[20],n;
while(1)
{//creating the main menu option
      printf("\n==Main Menu==\n1.insert\n2.extractmax\n3.display\n4.exit\n");
      printf("Enter your choice:");
      scanf("%d",&n);
      if(n==1){
            int num;
            printf("Input a number:");
            scanf("%d",&num);
            insert(array,num);
      }
      else if(n==2)
            deleteRoot(array,array[0]);
      else if(n==3)
        {
            printf("Elements are:");
            printArray(array,size);
        }
      else if(n==4)
      {
          printf("\nbye");
          break;
      }
      printf("\nEnter any key to go to main menu");
}

}


Related Solutions

write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
C language Write a program in C to implement Queue and its operation (like create, insert,...
C language Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new node.Example of quick sort below. Adopt to your program. #include <iostream> voidquickSort(inta[], intfirst, intlast); intpivot(inta[], intfirst, intlast); voidswap(int& a, int& b); voidswapNoTemp(int& a, int& b); voidprint(intarray[], constint& N); usingnamespacestd; intmain() { inttest[] = { 7, -13, 1, 3, 10, 5, 2, 4 }; intN = sizeof(test)/sizeof(int); cout << "Size of test array :"<< N << endl; cout << "Before sorting : "<< endl; print(test,...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
Consider an almost-priority-queue data structure, which allows two operations: insert and extract almost min. extract almost...
Consider an almost-priority-queue data structure, which allows two operations: insert and extract almost min. extract almost min operation outputs either the first minimum or a second minimum item from the current structure (and doesn’t tell you which it outputted). Prove that not both these operations can be done in o(log n) time even if amortization is allowed.
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being served at the Department of Motor Vehicles. Start with 100 customers in a List. In a loop, generate a priority for each customer (1-5) In a second loop, add the users to a priority queue Print the List and the Priority Queue
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT