[1509] [Data Structure] Windows Message Queue

  1. #include
  2. #include
  3. #include
  4. #include
  5. using namespacestd;
  6. structnode
  7. {
  8. charname[1000];
  9. int parameter;
  10. < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"> int priority;
  11. < span style="margin:0px; padding:0px; border:none; color: black; background-color:inherit">intnum;//Something went wrong here. . . It should be sorted in order. . . Similar to the question in the queue done by HDU last time, li>
  12. friendbooloperator<(nodea,nodeb)
  13. {
  14. if(b.priority== a.prio rity)
  15. < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">return b.num< a .num;
  16. return b.priority
  17. };
  18. i ntmain()
  19. {
  20. priority_queueq;
  21. charstr[100];
  22. node temp;
  23. int num=0;
  24. while(~scanf(“%s”,str))
  25. {
  26. if(strcmp(str,“GET”)==0)
  27. {
  28. if(!q.empty())
  29. {
  30. temp = q.top() ;
  31. printf(“%s %d
    ,temp.name,temp.parameter) ;
  32. q.pop();
  33. else
  34. printf (“EMPTYQUEUE!
    );
  35. else
  36. {
  37. scanf(“%s%d%d”,temp.name,&temp.parameter,&temp.priority); li>
  38. temp.num=num++;
  39. q.push(temp);
  40. < /li>
  41. }

  1. #include
  2. #include
  3. #include
  4. #include
  5. usingnamespace std;  
  6. struct node  
  7. {  
  8.     char name[1000];  
  9.     int  parameter;  
  10.     int  priority;  
  11.     int  num;   //这里出错了。 . .应该加个先后顺序排序。 . .类似上次HDU做的队列里的一题,  
  12.     friend bool operator<(node a,node b)  
  13.     {  
  14.         if(b.priority == a.priorit y)  
  15.            return b.num < a.num;  
  16.   
  17.         return b.priority < a.priority;  
  18.     }  
  19. };  
  20. int  main()  
  21. {  
  22.     priority_queue  q;  
  23.     char str[100];  
  24.     node temp;  
  25.     int  num=0;  
  26.     while(~scanf(“%s”,str))  
  27.     {  
  28.         if(strcmp(str,“GET”)==0)  
  29.         {  
  30.             if(!q.empty())  
  31.             {  
  32.                 temp = q.top();  
  33.                 printf(“%s %d
    ,temp.name,temp.parameter);  
  34.                 q.pop();  
  35.             }  
  36.             else  
  37.                 printf(“EMPTY QUEUE!
    );  
  38.         }  
  39.         else  
  40.         {  
  41.             scan f(“%s %d %d”,temp.name,&temp.parameter,&temp.priority);  
  42.             temp.num=num++;  
  43.             q.push(temp);  
  44.         }  
  45.     }  
  46.   
  47.   
  48.   
  49. }  

Leave a Comment

Your email address will not be published.