以下是我的问题的两个相关功能,但请询问您是否需要任何其他功能来帮助我解决这个问题。该程序模拟具有指数到达时间、指数服务时间和 FCFS 调度规则的单个服务器队列。这是一项任务,我打算通过按 CPU_time 对队列列表进行排序来修改它以使用 SJF 调度。这是我对冒泡排序的尝试,但是当它运行时,它并没有进入 do-while 循环。在将链表添加到链表时对其进行排序的最佳方法是什么?
/*********************************************************************/
/* Name: Puton_queue */
/* Description */
/* This procedure inserts a customer at the end of the given */
/* queue. The parameters are as follows: */
/* pqueue - pointer to the queue. */
/* pcust - index of the customer to be inserted. */
/* The procedure performs the following steps: */
/* 1 - get a free node for the customer. */
/* 2 - inset the node ar the end of the queue. */
/* 2a - into an empty queue */
/* 2b - normal insertion */
/*********************************************************************/
void Puton_queue(struct Queue_struct *pqueue, struct Custs *pcust, struct Custs *CPU_time)
{
struct Queue *newnode;
/* get an new node */
printf(" My CPUTIME is %ld:\n", CPU_time);
newnode = (struct Queue *) malloc(sizeof(struct Queue));
/* now loc is the index of a free node in queue */
/* put information in the node */
newnode->cust_index = pcust;
newnode->CPU_time=CPU_time;
newnode->next = NULL;
/* check to see if the queue is initially empty */
if(pqueue->q_last == NULL)
{
pqueue->q_head = newnode;
pqueue->q_last = newnode;
return;
}
/* otherwise add it to the end of the queue and relink */
pqueue->q_last->next = newnode;
pqueue->q_last = newnode;
int i;
bool swapped=TRUE;
struct Queue *currentnode;
struct Queue *lastnode = NULL;
do
{
swapped = TRUE;
currentnode = pqueue->q_head;
printf("nodeIME is %ld:\n", currentnode->CPU_time);
while(currentnode->next != lastnode)
{
if(currentnode->CPU_time < currentnode->next->CPU_time)
{
swap(currentnode, currentnode->next);
swapped = FALSE;
}
}
lastnode = currentnode;
}
while(swapped);
return;
}
/*********************************************************************/
/* Name: swap */
/* Description */
/* This function is used to swap two nodes in the queue list */
/*********************************************************************/
void swap(struct Queue *a, struct Queue *b)
{
struct Queue *tem;
tem = a->cust_index;
a->cust_index = b->cust_index;
b->cust_index = tem;
tem = a->next;
a->next = b->next;
b->next = tem;
}