【C語言】【數據結構】回圈佇列的基本操作(建立、入隊、出隊、隊長、隊頭、遍歷、應用)

2020-08-11 16:15:32

建立空的回圈佇列,並實現入隊、出隊、返回佇列的長度、返回隊頭元素、佇列的遍歷等基本演算法。

#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
#define MAXQSIZE 100

typedef int Status;

typedef int QElemType;
typedef struct{
	QElemType *base;
	int front, rear;
}SqQueue;

Status InitQueue(SqQueue & );				//空佇列
Status EnQueue(SqQueue & , QElemType );		//入隊
Status DeQueue(SqQueue & , QElemType & );	//出隊
int QueueLength(SqQueue );					//佇列長度
Status GetHead(SqQueue , QElemType & );		//隊頭
Status QueueTraverse(SqQueue );				//遍歷

//應用
Status bank_simulation();		//銀行客戶平均等待時間



int main()
{
	SqQueue Q;
	int a;
	QElemType e;
	
	if(!InitQueue(Q)) printf("Create Error!\n");
	while(1)
	{
		printf("1:Enter\n2:Delete\n3:Get the front\n4:Return the length of the queue\n5:Load the queue\n0:Exit\nPlease choose:\n");
		scanf("%d",&a);
		switch(a)
		{
			case 1: printf("Enter the element: ");
					scanf("%d", &e);
					if(!EnQueue(Q,e)) printf("Enter Error!\n\n");
					else printf("The element is %d successfully entered!\n\n", e);
					break;
			case 2: if(!DeQueue(Q,e)) printf("Delete Error!\n\n");
					else printf("The element is %d successfully deleted!\n\n", e);
					break;
			case 3: if(!GetHead(Q,e)) printf("Get Head Error!\n\n");
					else printf("The head of the queue is %d!\n\n", e);
					break;
			case 4: printf("The length of the queue is %d\n\n", QueueLength(Q));
					break;
			case 5: printf("The queue is ");
					QueueTraverse(Q);
					printf("\n");
					break;
			case 0: return OK;
			
		}
	}
}

Status InitQueue(SqQueue &Q)
{
	Q.base = (QElemType *) malloc (MAXQSIZE * sizeof(QElemType));
	if(!Q.base) return ERROR;
	Q.front = Q.rear =0;
	return OK;
}

Status EnQueue(SqQueue &Q, QElemType e)
{
	if((Q.rear+1)%MAXQSIZE == Q.front) return ERROR;
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear+1) % MAXQSIZE;
	return OK;
}

Status DeQueue(SqQueue &Q, QElemType &e)
{
	if(Q.front == Q.rear) return ERROR;
	e = Q.base[Q.front];
	Q.front = (Q.front+1) % MAXQSIZE;
	return OK;
}

int QueueLength(SqQueue Q)
{
	return((Q.rear - Q.front + MAXQSIZE) % MAXQSIZE);
}

Status GetHead(SqQueue Q, QElemType &e)
{
	if(Q.front == Q.rear) return ERROR;
	e = Q.base[Q.front];
	return OK;
}

Status QueueTraverse(SqQueue Q)
{
	int i;
	
	if(Q.front == Q.rear) printf("The queue is empty!\n");
	else
		for(i=Q.front; i!=Q.rear; i=(i+1)%MAXQSIZE)
			printf("%d ", Q.base[i]);
	printf("\n");
	return OK;
}

佇列的應用:銀行客戶平均等待時間(一個視窗)
到達時間 arrivaltime
辦理業務時間 duration
離開時間 departure
等待時間 waittime
總等待時間 total

輸入格式
第一行:一天內的客戶總人數n
第二行:第一個客戶的到達時刻和需要辦理業務的時間
第三行:第二個客戶的到達時刻和需要辦理業務的時間
……
第n行:第n - 1個客戶的到達時刻和需要辦理業務的時間
第n + 1行:第n 個客戶的到達時刻和需要辦理業務的時間

輸出格式
第一行:所有客戶的平均等待時間(精確到小數點後2位)

Status bank_simulation()
{
	SqQueue Q;
	int n, i;
	int arrivaltime, duration, departure, waittime, total=0;
	float agv;
	
	if(!InitQueue(Q)) printf("Create Error!\n");
	printf("請輸入一天內的客戶總人數: ");
	scanf("%d", &n);
	for(i=1; i<=n; i++)
	{
		printf("請輸入第%d個客戶的到達時刻和需要辦理業務的時間: \n", i);
		scanf("%d %d", &arrivaltime, &duration);
		EnQueue(Q, arrivaltime);
		EnQueue(Q, duration);
	}
	DeQueue(Q, arrivaltime);
	DeQueue(Q, duration);
	departure = duration + arrivaltime;
	while(Q.front != Q.rear)
	{
		DeQueue(Q, arrivaltime);
		waittime = departure - arrivaltime;
		DeQueue(Q, duration);
		departure += duration;
		total += waittime;
	}
	agv = total*1.0 / n ;
	printf("平均等待時間:%.2f\n", agv);
}