实验二 栈和队列

实验题6:编写病人看病模拟程序
编写一个程序,反映病人到医院排队看医生的情况。在病人排队过程中主要重复下面两件事。
(1) 病人到达诊室,将病历本交给护士,排到等待队列中候诊。
(2) 护士从等待队列中取出下一位病人的病历,该病人进入诊室就诊。
要求模拟病人等待就诊这一过程,程序采用菜单方式,其选项及功能说明如下:

  1. 排队——输入排队病人的病历号,加入到病人排队队列中;
  2. 就诊——病人排队队列中最前面的病人就诊,并将其从队列中删除;
  3. 查看排队——从队首到队尾列出所有的排队病人的病历号;
  4. 不再排队,余下依次就诊——从队首到队尾列出所有的排队病人的病历号,并退出运行。
  5. 下班——退出运行。
#include "malloc.h"
#include "iostream"
using namespace std;

typedef struct qnode
{
	int number;
	struct qnode *next;
}DataNode;
typedef struct
{
	DataNode *front;
	DataNode *rear;
}LinkQuNode;

void Create(LinkQuNode *&q)
{
	q=(LinkQuNode*)malloc(sizeof(LinkQuNode));
	q->front=q->rear=NULL;
}

void Begin(LinkQuNode *&q,int e)
{
	DataNode *p;
	p=(DataNode*)malloc(sizeof(LinkQuNode));
	p->number=e;
	p->next=NULL;
	if(q->rear==NULL)
	{
		q->front=q->rear=p;
	}
	else
	{
		q->rear->next=p;
		q->rear=p;
	}
}

void See(LinkQuNode *&q,int &e)
{
	DataNode *t;
	t=q->front;
	e=t->number;
	if(q->rear==NULL)
	{
		cout<<"等待队列中已无病人!"<<endl;
	}
	else if(q->front==q->rear)
	{
		q->front=q->rear=NULL;
	}
	else
	{
		q->front=q->front->next;
	}
	free(t);
}

void LookAll(LinkQuNode *&q)
{
	DataNode *t;
	if(q->front==NULL)
	{
		cout<<"队列中已无病人!"<<endl;
	}
	else
	{
		t=q->front;
		cout<<"队列中病人:"<<endl;
		while(t!=NULL)
		{
			cout<<t->number<<endl;
			t=t->next;
		}
	}
}

void Destroy(LinkQuNode *&q)
{
	DataNode *pre=q->front,*p;
	if(pre!=NULL)
	{
		p=pre->next;
		while(p!=NULL)
		{
			free(pre);
			pre=p;p=p->next;
		}
		free(pre);
	}
	free(q);
}

int main()
{
	
	LinkQuNode *line;
	Create(line);
	int choose,number1,number2;
	while(1)
	{
		cout<<"请输入要执行的操作序号:"<<endl;
		cout<<"1.排队"<<endl<<"2.就诊"<<endl<<"3.查看排队"<<endl<<"4.不再排队,余下依次就诊"<<endl<<"5.下班"<<endl;
		cin>>choose;
		if(choose==1)
		{
			cout<<"请输入该病人病历号:";
			cin>>number1;
			Begin(line,number1);
		}
		else if(choose==2)
		{
			See(line,number2);
			cout<<"病历号为"<<number2<<"的病人开始就诊。"<<endl; 
		}
		else if(choose==3)
		{
			LookAll(line);
		}
		else if(choose==4)
		{
			LookAll(line);
			Destroy(line);
			exit(0);
		}
		else if(choose==5)
		{
			Destroy(line);
			exit(0);
		}
		else cout<<"格式有误,请重新输入!"<<endl;
		cout<<endl;
	}
	return 0;
}

实验题7:求解栈元素排序问题
编写一个程序,按升序对一个字符栈进行排序,即最小元素位于栈顶,最多只能使用一个额外的栈存放临时数据,并输出栈排序过程。

#include "malloc.h"
#include "iostream"

using namespace std;

typedef struct
{
	int data[4];
	int top;
}SqStack;

void InitStack(SqStack *&s)
{
	s=(SqStack*)malloc(sizeof(SqStack));
	s->top=-1;
}

void DestroyStack(SqStack *&s)
{
	free(s);
}

bool StackEmpty(SqStack *&s)
{
	return(s->top==-1);
}

void Push(SqStack *&s,int e)
{
	s->top++;
	s->data[s->top]=e;
}

void Pop(SqStack *&s,int &e)
{
	e=s->data[s->top];
	s->top--;
}

void GetTop(SqStack *s,int &e)
{
	e=s->data[s->top];
}

void Stack(SqStack *&s)
{
	SqStack *tmpst;
	InitStack(tmpst);
	int e,e1;
	while(!StackEmpty(s))
	{
		Pop(s,e);
		cout<<"原栈中元素"<<e<<"出栈;"; 
		while(!StackEmpty(tmpst))
		{
			GetTop(tmpst,e1);
		    cout<<"临时栈中取栈顶元素"<<e1<<",";
			if(e1>e)
			{
				cout<<"因"<<e1<<">"<<e<<",临时栈中"<<e1<<"退栈,";
				Pop(tmpst,e1);
				cout<<"元素"<<e1<<"进入原栈。"<<endl;
				Push(s,e1); 
			} 
			else
			{
				cout<<"因"<<e1<<"<"<<e<<",";
				break; 
			}
		}
		Push(tmpst,e);
		cout<<"元素"<<e<<"进入临时栈。"<<endl;
	}
	while(!StackEmpty(tmpst))
	{
		Pop(tmpst,e);
		Push(s,e);
	}
	DestroyStack(tmpst);
}

int main()
{
	int e,n1=3,n2=1,n3=4,n4=2;
	SqStack *s;
	InitStack(s);
	cout<<"元素3,1,4,2依次进栈。"<<endl;
	Push(s,n1);
	Push(s,n2);
	Push(s,n3);
	Push(s,n4);
	cout<<"排序过程:"<<endl;
	Stack(s);
	cout<<"出栈序列为:";
	while(!StackEmpty(s))
	{
		Pop(s,e);
		cout<<e<<" ";
	}
	cout<<endl;
	DestroyStack(s);
}

实验题9:编写停车场管理程序
编写满足以下要求的停车场管理程序,设停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。
汽车在停车场内按车辆到达时间的先后顺序依次由南向北排列(大门在最北端,最先到达的第一辆车停放在停车场的最南端),若停车场内已经停满n辆车,则后来的汽车只能在门外的便道(即候车场上)等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出停车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入停车场,每辆停放在停车场的车在它离开停车场时必须按停留的时间长短交纳费用。

#include "malloc.h"
#include "iostream"

using namespace std;

typedef struct
{
	int number[3];
	int time[3];
	int top;
}SqStack;

typedef struct
{
	int number[4];
	int front;
	int rear;
}SqQuene;

//停车场(栈)

void InitStack(SqStack *&s)
{
	s=(SqStack*)malloc(sizeof(SqStack));
	s->top=-1;
} 
bool StackEmpty(SqStack *s)
{
	return(s->top==-1);
}
bool StackFull(SqStack *s)
{
	return(s->top==2);
}
bool Push(SqStack *&s,int e1,int e2)
{
	if(s->top==2)
	{
		return false;
	}
	s->top++;
	s->number[s->top]=e1;
	s->time[s->top]=e2;
	return true;
}
bool Pop(SqStack *&s,int &e1,int &e2)
{
	if(s->top==-1)
	{
		return false;
	}
	e1=s->number[s->top];
	e2=s->time[s->top];
	s->top--;
	return true;
}
void DispStack(SqStack *s)
{
	for(int i=s->top;i>=0;i--)
	{
		cout<<s->number[i]<<endl;
	}
}

//候车便道(队)
void InitQueue(SqQuene *&q)
{
	q=(SqQuene *)malloc(sizeof(SqQuene));
	q->front=q->rear=0;
}
bool QueueEmpty(SqQuene *q)
{
	return(q->front==q->rear);
}
bool QueueFull(SqQuene *q)
{
	return((q->rear+1)%4==q->front);
}
bool enQueue(SqQuene *&q,int &e)
{
	if((q->rear+1)%4==q->front)
	return false;
	q->rear=(q->rear+1)%4;
	q->number[q->rear]=e;
	return true;
}
bool deQueue(SqQuene *&q,int &e)
{
	if(q->front==q->rear)
	return false;
	q->front=(q->front+1)%4;
	e=q->number[q->front];
	return true;
}
void DisQueue(SqQuene *q)
{
	int i=(q->front+1)%4;
	cout<<q->number[i]<<endl;
	while((q->rear-i+4)%4>0)
	{
		i=(i+1)%4;
		cout<<q->number[i]<<endl;
	}
}

int main()
{
	int comm,i,j;
	int no,e1,time,e2;
	SqStack *st,*st1;
	SqQuene *qu;
	InitStack(st);
	InitStack(st1);
	InitQueue(qu);
	while(1)
	{
		cout<<"请输入指令:1.车辆到达 2.车辆离开 3.停车场车位情况 4.候车场车位情况 0.退出"<<endl;
		cin>>comm;
		if(comm==1)
		{
			cout<<"请输入车辆号码:";
			cin>>no;
			cout<<"请输入到达时间:";
			cin>>time;
			if(!StackFull(st))
			{
				Push(st,no,time);
				cout<<"停车场位置:"<<st->top+1<<endl; 
			} 
			else if(!QueueFull(qu))
			{
				enQueue(qu,no);
				cout<<"候车场位置:"<<qu->rear<<endl; 
			}
			else cout<<"候车场已满,无法停车。"<<endl;
		}
		else if(comm==2)
		{
			cout<<"请输入车辆号码:";
			cin>>no;
			cout<<"请输入离开时间:";
			cin>>time;
			for(i=0;i<=st->top&&st->number[i]!=no;i++);
			if(i>st->top)
			cout<<"无此号码汽车!"<<endl;
			else
			{
				for(j=i;j<=st->top;j++)
				{
					Pop(st,e1,e2);
					Push(st1,e1,e2);
				}
				Pop(st,e1,e2);
				cout<<"该汽车停车费用为"<<(time-e2)*2<<endl;
				while(!StackEmpty(st1))
				{
					Pop(st1,e1,e2);
					Push(st,e1,e2);
				} 
				if(!QueueEmpty(qu))
				{
					deQueue(qu,e1);
					Push(st,e1,time);
				} 
			}
		}
		else if(comm==3)
		{
			if(!StackEmpty(st))
			{
				cout<<"停车场中车辆为:"<<endl;
				DispStack(st); 
			}
			else cout<<"停车场中目前无车辆!"<<endl;
		}
		else if(comm==4)
		{
			if(!QueueEmpty(qu))
			{
				cout<<"候车场中车辆为:"<<endl;
				DisQueue(qu); 
			}
			else cout<<"候车场中目前无车辆!"<<endl;
		}
		else if(comm==0)
		    exit(0);
		else
		    cout<<"格式有误,请重新输入!"<<endl;
		cout<<endl;
	}
}

仅作留档。

发布了30 篇原创文章 · 获赞 12 · 访问量 865

猜你喜欢

转载自blog.csdn.net/weixin_43893854/article/details/104326143