⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wqueue.h

📁 关于一个五层楼的电梯模型,关于栈与队列的应用,内有实验报告.
💻 H
字号:
//单链队列——队列的链式存储结构
typedef Client *QElemType;
//等候队列
typedef struct QNode {
	QElemType		data;
	struct QNode	*next;
}QNode,*QueuePtr;
typedef struct {
	QueuePtr	front;	//队头指针
	QueuePtr	rear;	//队尾指针
}WQueue;
//等待队列的基本操作
Status InitQueue(WQueue &Q) {
	//构造一个空队列Q
	Q.front=Q.rear=new QNode;
	if(!Q.front) return OVERFLOW;//分配存储失败
	Q.front->next=NULL;
	Q.front->data=NULL;
	return OK;
}

Status DestroyQueue(WQueue &Q) {
	//销毁队列Q
	while(Q.front) {
		Q.rear=Q.front->next;
		if(Q.front->data) DestoryClient(Q.front->data);
		delete Q.front;
		Q.front=Q.rear;
	}
	return OK;
}

Status EnQueue(WQueue &Q,QElemType e) {
	//插入元素e为Q的新的队尾元素
	QueuePtr p;
	p=new QNode;
	if(!p) return OVERFLOW;
	p->data=e;p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	return OK;
}
Status DeQueue(WQueue &Q,QElemType &e) {
	//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;
	//否则返回ERROR
	QueuePtr p;
	if(Q.front==Q.rear) return ERROR;
	p=Q.front->next;
	e=p->data;
	Q.front->next=p->next;
	if(Q.rear==p) Q.rear=Q.front;
	delete p;
	return OK;
}

Status QueueEmpty(WQueue Q) {
	//判断队列是否为空
	if(Q.front==Q.rear) return TRUE;
	else	return FALSE;
}

Status QDelNode(WQueue &Q,QueuePtr p) {
	//删除队列中p指向的结点的下一个结点
	QueuePtr q;
	if(p==NULL||p->next==NULL) return ERROR;
	q=p->next;
	p->next=q->next;
	if(p->next==NULL) Q.rear=p;
	DestoryClient(q->data);
	delete q;
	return OK;
}

Status CGiveUp(WQueue &Q,int floor) {
	//删除放弃等待的乘客
	QueuePtr p;
	p=Q.front;
	if(p->next!=NULL) 
		if(p->next->data->GivepuTime==0&&floor!=p->next->data->Infloor) {
			PrintClientInfo(*(p->next->data),GiveUp);
			TotalTime+=Time-CInTime(*(p->next->data));
			QDelNode(Q,p);//将放弃等待的人删除
			GiveUpNumber++;
		}
		else	p->next->data->GivepuTime--;

	return OK;
}

void PrintQueue(WQueue Q) {
	//输出队列
	QueuePtr q;
	int count=0;
	if(Q.front->next==NULL) goto end;
	q=Q.front->next;
	while(q!=NULL) {
		cout<<setw(3)<<q->data->ClinetID<<' ';
		q=q->next;
		count++;
	}
end:
	while(count++<=4) cout<<"    ";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -