7-26 Windows Message Queuing (25 points)
Message Queuing is the foundation of Windows systems. For each process, the system maintains a message queue. If a particular event occurs in the process, such as a mouse click, text changes, etc., the system will add this message to the queue. At the same time, if the queue is not empty, the process loops through the queue to get the message in the priority order. Note that a low priority value means high priority. Please edit the program to impersonate the message queue, add the message to the queue, and get the message from the queue.
Input format:
The input first gives a positive integer n (≤10?). 5?? ), followed by n rows, each line given an instruction- GET
or PUT
, respectively, to take a message out of the queue or add a message to the queue. If the instruction is PUT
, then there is a message name, and a positive integer indicates the priority of the message, and the smaller the number, the higher the priority level. The message name is a string that is not longer than 10 characters and does not contain spaces; The topic guarantees that messages in the queue have no duplicate priority and at least one input GET
.
Output format:
For each GET
instruction, the name and parameters of the highest priority message in the message queue are output in a single line. If there is no message in the message queue, the output EMPTY QUEUE!
. PUT
there is no output for the instruction.
Input Sample:
9PUT msg1 5PUT msg2 4GETPUT msg3 2PUT msg4 4GETGETGETGET
Sample output:
msg2msg3msg4msg1EMPTY QUEUE!
Problem-solving ideas: The main point is to build a small top heap, difficult is the output after the reconstruction, need to simulate the process
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5typedefstructNode *node;6 structNode7 {8 Charmes[ One];9 intPriority ;Ten }; One A struct - { -Node heap[100005]; the intnum; - } Heap; - - voidPut (); + voidGet (); - + intMain () A { at intN; -scanf"%d",&n); -heap.heap[0] = (node)malloc(sizeof(structNode)); -heap.heap[0]->priority =-1; -Heap.num =0; - in while(n--) - { to Charop[4]; + GetChar (); -scanf"%s", op); the Switch(op[0]) * { $ Case 'P' :Panax Notoginseng Put (); - Break; the Case 'G' : + Get (); A Break; the default : + Break; - } $ } $ - return 0; - } the - Wuyi voidPut () the { - //read the data and build a small top heap Wu inti; -Node temp = (node)malloc(sizeof(structNode)); Aboutscanf"%s%d",temp->mes,&temp->Priority ); $ for(I=++heap.num; heap.heap[i/2]->priority > temp->priority; i=i/2) - { -Heap.heap[i] = heap.heap[i/2]; - } AHeap.heap[i] =temp; + } the - voidGet () $ { the //output data, rebuild top heap the inti; the the if(heap.num<1) - { inprintf"EMPTY queue!\n"); the return ; the } Aboutprintf"%s\n", heap.heap[1]->mes); the for(i=1; i*2<heap.num;) the { the if(i*2+12+1]->priority2]->Priority ) + { - //If there are two root nodes, and the right node priority number is less than the left node precedence number the if(heap.heap[i*2+1]->priorityPriority )Bayi { theHeap.heap[i] = heap.heap[i*2+1]; thei=i*2+1; - } - Else Break; the } the Else the { the if(heap.heap[i*2]->priority < heap.heap[heap.num]->Priority ) - { theHeap.heap[i] = heap.heap[i*2]; theI *=2; the }94 Else Break; the } the } theHeap.heap[i] = heap.heap[heap.num--];//fill the last element with a vacancy98}
7-26 Windows Message Queuing (25 points) (Heap sort)