diff --git a/代码 b/代码 index 0ba075b..fdd3daf 100644 --- a/代码 +++ b/代码 @@ -524,3 +524,180 @@ return OK; } +Status InitQueue(LinkQueue& Q)//链队列的初始化 + +{ + +Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); + +if (!Q.front) + +{ + +exit(OVERFLOW); + +} + +Q.front->next = NULL; + +return OK; + +} + +Status EnQueue(LinkQueue& Q, QElemType e)//入队 + +{ + +QNode* p = (QueuePtr)malloc(sizeof(QNode)); + +if (!p) + +{ + +exit(OVERFLOW); + +} + +p->data = e; + +p->next = NULL; + +Q.rear->next = p; + +Q.rear = p; + +return OK; + +} + +Status DeQueue(LinkQueue& Q, QElemType& e)//出队 + +{ + +if (Q.front == Q.rear) + +{ + +return ERROR; + +} + +QNode* p = Q.front->next; + +e = p->data; + +Q.front->next = p->next; + +if (Q.rear == p)//注意这里要考虑到,当队列中最后一个元素被删后,队列尾指针也丢失了,因此需对队尾指针重新复制(指向头结点) + +{ + +Q.rear = Q.front; + +} + +free(p); + +return OK; + +} + +int QueueLength(LinkQueue Q)//返回队列的长度 + +{ + +int count = 0; + +QNode* p = Q.front->next; + +while (p) { + +p = p->next; + +count++; + +} + +return count; + +} + +Status GetHead(LinkQueue Q, QElemType& e)//获取队头元素 + +{ + +if (Q.front == Q.rear) + +{ + +return ERROR; + +} + +e = Q.front->next->data; + +} + +Status QueueEmpty(LinkQueue Q)//判断队列是否为空 + +{ + +if (Q.front == Q.rear) + +{ + +return TRUE; + +} + +return FALSE; + +} + +void PrintQueue()//打印队列 + +{ + +//打印当前队列   + +int i; + +for (i = 1; i <= 4; i++) { + +printf("窗口 %d 有 %d 个客户:", i, QueueLength(q[i])); + +QueueTraverse(q[i]); + +} + +printf("\n"); + +} + +Status QueueTraverse(LinkQueue Q)//遍历队列Q   + +{ + +QNode* p = Q.front->next; + +if (!p) { + +printf("--Is empty.\n"); + +return ERROR; + +} + +while (p) { + +printf("(到达时刻 %d min 办理业务需要花费 %d min) ", p->data.ArrivalTime, p->data.Duration); + +p = p->next; + +} + +printf("\n"); + +return OK; + +} \ No newline at end of file