Data Structure course design question 12_computer College Student Union printer (priority queue)

Source: Internet
Author: User

From: http://blog.csdn.net/svitter

Question 12: Computer College Student Union printer (priority queue)

James complained that the student union printer does not comply with the FIFO principle. Many of the students who printed the printer later than him printed the printer first. Five minutes ago, James's file was the next candidate, and now James's file is behind. Students of the Student Union explained to James that the student union printer is not a traditional queue method, but a custom priority queue method: each file to be printed is given a priority from 1 to 9 (9 highest, 1 lowest ). The printing rules are as follows:

Extract the printed file F from the queue;

If there is a file in the queue with a priority higher than F, the F will not be printed, and the F will be placed at the end of the queue; otherwise, the F will be printed.

James knows how long it will take to print out his file after printing the new rule. If no file is printed for one minute, James's file is in the print queue, there are no new files in the print queue. Please help James calculate how long he will wait. We recommend that you have 1 person.


Filtering code with small elements added


Priorityqueue. cpp:

// ================================================ ==================================================================== // Name: priorityqueue. CPP // Author: vit // version: // copyright: Your copyright notice // Description: Hello world in C ++, ANSI-style // ========================================== ========================================================== = # include <iostream> # include <stdio. h> # include "queue. H "using namespace STD; int main () {queue * q; q = new queu E (); int I; int n, m, Val, num; int mval; node * P; freopen ("test", "r", stdin); While (~ Scanf ("% d", & N, & M) {num = 1; for (I = 1; I <= N; I ++) {scanf ("% d", & Val); // printf ("Number % d is adding queue... \ n ", I); P = new node (Val, null, I); if (I = m) {mval = val ;} q-> push (p);} Q-> check (mval); // printf ("the queue has been added. \ N "); While (! Q-> isempty () {P = Q-> top (); // printf ("% d \ n", p-> num is printed currently ); if (p-> num = m) {printf ("James prints at % d minute. \ N ", num) ;}q-> POP (); num ++ ;}} return 0 ;}


Queue. cpp:

/** Queue. CPP ** created on: 2014 äê 6 *â 15â {* Author: Vit */# include <stdio. h> # include "queue. H "queue: Queue () {// todo auto-generated constructor stubthis-> front = This-> rear = NULL;} bool queue: isempty () {return this-> rear = NULL;} void queue: Push (node * n) {If (isempty ()) {This-> front = This-> rear = N;} else {This-> rear-> next = N; this-> rear = N;} return;} void queue:: Pop () {If (isempty () {return;} node * P = This-> front; If (this-> front = This-> rear) {P = This-> front; this-> front = This-> rear = NULL ;} else {P = This-> front; this-> front = This-> front-> next;} Delete P;} node * queue: Top () {If (isempty () {return NULL;} int value = This-> front-> value; node * P = This-> front; while (p-> next! = NULL) {If (p-> next-> value <= value) P = p-> next; else {This-> rear-> next = This-> front; this-> rear = P; this-> front = p-> next; P-> next = NULL; P = This-> front; value = p-> value ;}} return this-> front;} void queue: Check (INT mval) {node * P = This-> front; node * temp; while (p-> next! = NULL) {If (p-> next-> value <mval) {temp = p-> next; P-> next = p-> next; delete temp;} // P points to the next node P = p-> next;} queue ::~ Queue () {// todo auto-generated destructor stubwhile (! Isempty () {node * temp; temp = This-> front; this-> front = This-> front-> next; Delete temp ;}}



Queue. h:

/** Queue. h ** created on: June 15, 2014 * Author: Vit */# ifndef queue_h _ # define queue_h_class node {public: int value; int num; node * Next; node () {This-> value = 0; this-> num = 0; this-> next = 0;} node (INT value, node * Next, int num) {This-> value = value; this-> next = next; this-> num = num ;}}; class queue {public: Queue (); Virtual ~ Queue (); // method void push (node * n); void POP (); node * Top (); bool isempty (); void check (INT mval ); // member node * front; node * rear;}; # endif/* queue_h _*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.