Question

In: Computer Science

write C program to implement the priority queue with the operation insert

write C program to implement the priority queue with the operation insert

Solutions

Expert Solution

#include<stdio.h>
#define N 20
int Q[N],Pr[N];
int r = -1,f = -1;
void enque(int data,int p)//Enqueue function to insert data and its priority in queue
{
        int i;
        if((f==0)&&(r==N-1)) //Check if Queue is full
                printf("Queue is full");
        else
        {
                if(f==-1)//if Queue is empty
                {
                        f = r = 0;
                        Q[r] = data;
                        Pr[r] = p;

                }
                else if(r == N-1)//if there there is some elemets in Queue
                {
                        for(i=f;i<=r;i++) { Q[i-f] = Q[i]; Pr[i-f] = Pr[i]; r = r-f; f = 0; for(i = r;i>f;i--)
                                {
                                        if(p>Pr[i])
                                        {
                                                Q[i+1] = Q[i];
                                                Pr[i+1] = Pr[i];
                                        }
                                        else
                                                break;
                                        Q[i+1] = data;
                                        Pr[i+1] = p;
                                        r++;
                                }
                        }
                }
                else
                {
                        for(i = r;i>=f;i--)
                        {
                                if(p>Pr[i])
                                {
                                        Q[i+1] = Q[i];
                                        Pr[i+1] = Pr[i];        
                                }
                                else
                                        break;
                        }
                        Q[i+1] = data;
                        Pr[i+1] = p;
                        r++;
                }       
        }

}
void print() //print the data of Queue
{
int i;
        for(i=f;i<=r;i++)
        {
                printf("\nElement = %d\tPriority = %d",Q[i],Pr[i]);
        }
}
 
int main()
{
        int opt,n,i,data,p;
        printf("Enter Your Choice:-");
        do{
                printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n0 for Exit \n");
                scanf("%d",&opt);
                switch(opt){
                        case 1:
                                printf("\nEnter the number of data");
                                scanf("%d",&n);
                                printf("\nEnter your data and Priority of data \n");
                                i=0;
                                while(i<n){
                                        scanf("%d %d",&data,&p);
                                        enque(data,p);
                                        i++;
                                }
                                break;
                        case 2:
                                print();
                                break;
        
                        case 0:
                                break;
                        default:
                                printf("\nIncorrect Choice");

                }
        }while(opt!=0);
        return 0;
}

Output screen:

if you have any doubt regarding this question then fell free to ask in comment section.


Related Solutions

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==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
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...
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
In C#, write a method that adds a Huffman tree (implemented via a minimum priority queue)...
In C#, write a method that adds a Huffman tree (implemented via a minimum priority queue) to a dictionary table. Include any necessary fields/properties, but keep the functionality within one single method.
Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT