Application of Experiment Four Cohorts

1. Experimental purpose
1. To be more familiar with and master the methods and steps of compiling, debugging and executing in the Dev-C++ environment.
2. Familiar with the chain/sequential storage structure of queue and its application.
2. Experiment content
Compile a simulation program for patient seeing a doctor.
In the process of patient queuing, two things are mainly repeated:
① The patient arrives at the consultation room, hand over the medical record to the nurse, and waits in the waiting queue.
②The nurse takes out the medical record of the next patient from the waiting queue, and the patient enters the consultation room for treatment.
The program adopts the menu mode, and its options and functions are explained as follows:
① Queue - input the medical record number of the patient in the queue, and join the patient queue.
②Visit a doctor - the first patient in the queue of patients will visit a doctor and delete it from the queue.
③ Check the queue - list the medical record numbers of all queued patients from the head of the queue to the tail of the queue.
④ Get off work - quit running.
3. Experimental process
1. Create a new project (the method is the same as that of the experiment 1)
2. Create a new structure file "LinkQueue.h" (the method is the same as that of the experiment 1)
3. The representation and realization of the chain queue structure (the method is the same as that of the experiment 1)
4. The realization of the experiment content
(1) Build the operation menu; (idea: while + switch)
(2) In each case, call the corresponding operation respectively;
(3) Code compilation, debugging and running.

normal writing

#include <stdio.h>
#include <malloc.h>
typedef struct qnode
{
    
    
    int data;
    struct qnode *next;
} QNode;           

typedef struct
{
    
    
    QNode *front,*rear;
} QuType;           

void SeeDoctor()
{
    
    
	QNode *q;
    int sel,flag=1,find,no;
    QuType *qu;
    QNode *p;
    qu=(QuType *)malloc(sizeof(QuType));    
    qu->front=qu->rear=NULL;
    while (flag==1)                            
    {
    
    
        printf("1:排队\n2:就诊\n3:查看排队\n4.不再排队,余下依次就诊\n5:下班\n请选择:");
        scanf("%d",&sel);
        switch(sel)
        {
    
    
        case 1:
            printf("输入病历号:");
            do
            {
    
    
                scanf("%d",&no);
                find=0;
                p=qu->front;
                while (p!=NULL && !find)
                {
    
    
                    if (p->data==no)
                        find=1;
                    else
                        p=p->next;
                }
                if (find)
                    printf("输入的病历号重复,重新输入:");
            }
            while (find==1);
            p=(QNode *)malloc(sizeof(QNode));   
            p->data=no;
            p->next=NULL;
            if (qu->rear==NULL)               
            {
    
    
                qu->front=qu->rear=p;
            }
            else
            {
    
    
                qu->rear->next=p;
                qu->rear=p; 
            }
            break;
        case 2:
            if (qu->front==NULL)                
                printf("没有排队的病人!\n");
            else                               
            {
    
    
                p=qu->front;
                printf("病人%d就诊\n",p->data);
                if (qu->rear==p)  
                {
    
    
                    qu->front=qu->rear=NULL;
                }
                else
                    qu->front=p->next;
                free(p);
            }
            break;
        case 3:
            if (qu->front==NULL)           
                printf("没有排列的病人!\n");
            else                           
            {
    
    
                p=qu->front;
                printf("排队病人:");
                while (p!=NULL)
                {
    
    
                    printf("%d ",p->data);
                    p=p->next;
                }
                printf("\n");
            }
            break;
        case 4:
            if (qu->front==NULL)            
                printf("没有排列的病人!\n");
            else                            
            {
    
    
                p=qu->front;
                printf("病人按以下顺序就诊:");
                while (p!=NULL)
                {
    
    
                    printf("%d ",p->data);
                    p=p->next;
                }
                printf("\n");
            }
            flag=0;                         
            break;
        case 5:
            if (qu->front!=NULL)          
                printf("下班了!!\n");
            flag=0;                     
            break;
        }
    }
    p=qu->front;  //销毁队列
    while (p!=NULL)
    {
    
    
        q = p->next;
        free(p);
        p = q;
    }
}
int main()
{
    
    
    SeeDoctor();
    return 0;
}

Use operations that take advantage of queues

#include<iostream>
#include<queue>
using namespace std;

int main()
{
    
    
	queue<int> q;
	 while (1)                            
    {
    
    
    	int sel;
    	int bh;
    	int temp;
        printf("1:排队\n2:就诊\n3:查看排队\n5:下班\n=============请选择:");
        scanf("%d",&sel);
        switch(sel)
        {
    
    
        	case 1:
        	printf("输入病历号:");
        	scanf("%d",&bh);
        	q.push(bh);
        	break;
        case 2:
        	if(q.size()==0)
        	{
    
    
        		printf("没有人排队");
				break; 
			}
        	printf("现在请%d病人就诊",q.front());
        	q.pop();
        	break;
        case 3:
            for(int i=q.size();i>0;i--)
            {
    
    
               temp=q.front();
               printf("现在还有%d号排队\n",temp);
               q.pop();
               q.push(temp);
			}
            break;
        case 4:
            q = queue<int>();
            return 0;
		}
		printf("\n");
	}
 } 

Guess you like

Origin blog.csdn.net/weixin_52521533/article/details/123408615