简体   繁体   English

如何通过检查消息优先级来实现字符串优先级队列

[英]how to implement string priority queue with checking message priority

Messenger is used to send or receive text messages. Messenger 用于发送或接收文本消息。 When someone is offline a messenger maintains a buffer of messages which is delivered to the receiver when he gets online.当某人离线时,信使维护一个消息缓冲区,当他上线时将其传递给接收者。 The phenomena take place on simple timestamp phenomena, the message delivered earlier will be sent to the receiver first and the message received late will be delivered after it.该现象发生在简单的时间戳现象上,先传递的消息将首先发送给接收者,晚接收的消息将在它之后传递。 Sometime a message in the buffer may have higher priority so it should be delivered earlier on the higher priority.有时缓冲区中的消息可能具有更高的优先级,因此它应该以更高的优先级更早地传递。 Some of the messages are to be delivered on a particular day or a date are also in the same buffer.一些消息将在特定的一天或某个日期传递,也在同一缓冲区中。 Your task is to select a suitable data structure (Heap or Priority Queue) and implement the requirements mentioned above.你的任务是 select 一个合适的数据结构(堆或优先队列)并实现上面提到的要求。 You need to implement program which shows a user to be offline, display the messages, with a click or a key stroke make the user online and deliver/display the messages according to the mentioned criteria.您需要实施程序来显示用户离线、显示消息、单击或击键使用户在线并根据上述标准发送/显示消息。

I dont understand how to check priority我不明白如何检查优先级

Sounds to me like your code that orders the heap has to check two things when assigning priority.在我看来,您的命令堆的代码在分配优先级时必须检查两件事。 It checks the priority flag (the one that signals that a message must be delivered sooner than normal), and the timestamp.它检查priority标志(表示必须比正常情况更早传递消息的标志)和时间戳。

So your comparison function looks something like:所以你的比较 function 看起来像:

// returns -1, 0, 1 to indicate if msg1 is less than, equal to,
// or greater than msg2.
int compare(msg1, msg2)
{
    if (msg1.priority == true)
    {
        if (msg2.priority == false)
            return -1; // msg1 has priority flag set and msg2 doesn't
    }
    else if (msg2.priority == true)
        return 1; // msg2 has priority flag set and msg1 doesn't
    // At this point, we know that the priority flag is the same
    // for both messages.
    // So compare timestamps.
    if (msg1.timestamp < msg2.timestamp)
        return -1;
    if (msg1.timestamp == msg2.timestamp)
        return 0;
    return 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM