-1

Messenger is used to send or receive text messages. 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. 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

1 Answers1

0

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.

So your comparison function looks something like:

// 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;
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351