首页 > > 详细

C辅导 Priority Queue解析R编程、JSP设计辅导留学生

Introduction

/
messagequeue.c

Created on: Feb 1, 2017
Author: phil
/
#include
#include “messagequeue.h”
/
Add a number to the queue.
/
void enqueueMQ(MessageQueue queue, Message message) {
int i;
if(queue->numElements==queue->size)
{
Message tmp=calloc(queue->size+1000,sizeof(Message));
for(i=0;isize;i++)
{
tmp[i]=queue->messages[i];
}
free(queue->messages);
queue->messages=tmp;
queue->size+=1000;
}
queue->messages[queue->numElements]=message;
queue->numElements++;
}
/**
Remove and return the first element in the queue.
/
Message dequeueMQ(MessageQueue queue) {
int i;
Message ans=NULL;
if(queue->numElements>0)
{
ans=queue->messages[0];
i=0;
while(inumElements-1)
{
queue->messages[i]=queue->messages[i+1];
i++;
}
queue->numElements–;
}
return(ans);
}
/
Create a new message queue
/
MessageQueue createQueueMQ() {
MessageQueue ans=(MessageQueue*)malloc(sizeof(MessageQueue));
ans->size=1000;
ans->numElements=0;
ans->messages=(Message)calloc(ans->size,sizeof(Message));
return(ans);
}
/**
Deallocate the memory for the queue.
/
void deleteQueueMQ(MessageQueue queue) {
int i=0;
while(inumElements)
{
deleteMessage(queue->messages[i]);
i++;
}
free(queue->messages);
queue->numElements=0;
queue->size=0;
queue->messages=NULL;
//free(queue);
}
/
Get the current number of elements in the queue.
/
int getNumElementsMQ(MessageQueue* queue){
if(queue!=NULL)
{
return(queue->numElements);
}
else
{
return(0);
}
}
,Cunit
Requirement**
Priority Queue
A Priority Queue is a queue (first-in first-out) where elements are enqueued with a priority. The next element dequeued from a priority queue is the oldest one with
the highest priority. If there are no elements with the highest priority, then dequeue operation returns the oldest one with the second-highest priority.
One use of a priority queue is to implement a message priority queue, where certain messages have a higher priority and must be delivered sooner than any messages
with lower priorities. For example, message queues are used for inter-program communication and for managing print jobs.
Implement a MessagePriorityQueue that incudes a struct and functions specified in the “messagepriorityqueue.h” file in the “prog-assignment-2-template” repository.
Stubs for the functions are in the “messagepriorityqueue.c” source file. The “messagepriorityqueue.h” file also defines a Priority enum. The ordinal values of the priorities indicate the priority, with Highest (0) being the highest and Lowest (3) the lowest.
The MessagePriorityQueue manages a MessageQueue for each priority. The MessageQueue is a struct and uses functions that are specified in the “messagequeue.h” file.
A “messagequeue.c” file has stubs for the functions.
Messages are defined by the Message struct and functions specified in the “message.h” file. Stubs for the functions are in the file “message.c”.
A main program and unit tests are contained in the “messagepriorityqueue_main.c” file. Your implementation must pass all the unit tests.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!