Data Structure 체인 대기 열 (하나)
제목.
체인 대기 열 을 이용 하여 환자 의 진찰 을 실현 하 는 시 뮬 레이 션 프로그램.(1) 대기 열의 저장 구 조 를 정의 합 니 다.(2) 대열 의 초기 화, 빈 것 인지, 입 대 · 출 대 등 기본 조작 여 부 를 판단 한다.(3) 대기 열의 기본 조작 을 호출 하여 환자 의 진찰 시 뮬 레이 션 프로그램 은 대기 열, 진료, 조회, 탈퇴 등 기능 을 포함한다.
코드
//
#include
#include
#include
using namespace std;
//
//
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct LinkQueue
{
//
QueuePtr front;
//
QueuePtr rear;
}LinkQueue;
//
void InitQueue_L(LinkQueue &Q)
{
QueuePtr p=new QNode;
p->next=NULL;
Q.front=Q.rear=p;
}
//
void DestroyQueue_L(LinkQueue &Q)
{
// Front
while(Q.front)
{
//
//
//
Q.rear=Q.front->next;
delete Q.front;
Q.front=Q.rear;
}
}
//
void ClearQueue_L(LinkQueue &Q)
{
//
//
//
//
//
QueuePtr p,q;
p=Q.front->next;
while(p)
{
q=p;
p=p->next;
delete q;
}
Q.front->next=NULL;
Q.rear=Q.front;
}
//
int QueueLength_L(LinkQueue Q)
{
QueuePtr p;
p=Q.front;
int length;
length=0;
while(p->next)
{
length++;
p=p->next;
}
return length;
}
//
void GetHead_L(LinkQueue Q,int &e)
{
if(Q.front->next==NULL)
{
cout<<"The Queue is EMPTY."<<endl;
return;
}
else
{
e=Q.front->next->data;
return;
}
}
//
void EnQueue_L(LinkQueue &Q,int &e)
{
//
QueuePtr p;
p->data=e;
p->next=NULL;
//
Q.rear->next=p;
Q.rear=p;
return;
}
//
void DeQueue_L(LinkQueue &Q,int &e)
{
if(Q.front->next==NULL)
{
cout<<"The Queue is EMPTY."<<endl;
return;
}
QueuePtr p;
//p front
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
//
if(Q.rear==p)
{
Q.rear=Q.front;
}
delete p;
}
void SeeDoctor()
{
LinkQueue Q;
//
InitQueue_L(Q); // Q
int flag=1; // flag=1: ;=0:
char ch;
while(flag)
{
cout<<" :";
cin>>ch;
switch(ch)
{
case 'a' :
case 'A' :
cout<<" :";
int n;
cin>>n;
cout<<endl;
EnQueue_L(Q,n);//
break;
case 'n' :
case 'N' :
if(Q.front->next)
{
int n;
DeQueue_L(Q,n); //
cout<<" "<<n<<" "<<endl;
}
else
cout<<" 。"<<endl;
break;
case 's' :
case 'S' :
cout<<" :";
while(Q.front->next)
{//
int n;
DeQueue_L(Q,n);
cout<<n<<" ";
}
cout<<endl<<" !"<<endl;
flag=0;
break;
default:
cout<<" !"<<endl;
}
}
} // SeeDoctor
int main()
{
void SeeDoctor();
SeeDoctor();
return 0;
}