当前位置:首页 » 编程语言 » c语言用链队列完成看病候诊问题
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言用链队列完成看病候诊问题

发布时间: 2022-05-10 16:32:32

Ⅰ 用c语言编写医院排队看病系统,急啊!!!求大神!!

#include <stdio.h>
#include <malloc.h>

typedef int queuetype;
queuetype num=1;
typedef struct qnode
{
queuetype data;
struct qnode *next;
} QNode; //链队结点类型
typedef struct
{
QNode *front,*rear;
} QuType;


void initlqueue(QuType *L)
{
L->front=L->rear=NULL;
}

void pushlqueue(QuType *L,queuetype e)
{
QNode *p=(QNode*)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
if(!L->front)
{
L->front=p;
}
if(L->rear)
L->rear->next=p;
L->rear=p;
num++;
}
void deletlqueue(QuType *L)
{
if(L->front)
{
QNode *p;
p=L->front;
printf("第%d位选手已经就诊! ",p->data);
L->front=p->next;
if(!p)
L->rear=NULL;
else
free(p);
}
else
{

num=0;
printf("所有的病人都已就诊完毕! ");
}
}

void showqueueperson(QuType *L)
{
QNode *p=L->front;
printf("输出所有排队者的序号: ");
while(p)
{
printf(" %d ",p->data);
p=p->next;
}
if(!L->front)
printf("病人都已经看病完成! ");
}

void quikSee(QuType*L,queuetype e)
{
QNode *p=L->front,*q,*t;
while(p&&p->data!=e)
{
t=p;
p=p->next;
}
if(p->data==e)
{
printf("find!%d号即可进行诊疗! ",p->data);
q=t->next;
if(q->next)
t->next=q->next;

if(t->next==L->rear)
t->next=L->rear=NULL; free(q);

}
else
printf("队列中无此人!无需删除操作! ");

}


//链队类型
void SeeDoctor()
{
int sel,flag=1;
QuType *qu=(QuType*)malloc(sizeof(QuType));
queuetype quik=0;
initlqueue(qu);//创建空队
while (flag==1) //循环执行
{
printf("1:排队 2:就诊 3:查看排队 4.不再排队,余下依次就诊 5:下班 请选择:");
scanf("%d",&sel);
switch(sel)
{ //排队,入队
case 1: pushlqueue(qu,num); printf(" 挂号成功! "); break;
case 2: deletlqueue(qu);
printf(" ");break; //就诊,出队
case 3: showqueueperson(qu); break; //显示排队病人
case 4: {
printf("若您需要马上就诊,请输入您的号:");
scanf("%d",&quik);
quikSee(qu,quik);
printf(" ");
} break; //任意顺序就诊
case 5: {
printf("抱歉!已下班,请排队的病人明天再来就诊! ");//下班,明天就医!
flag=0; //退出
break;
}
default : printf("输入错误,请从新输入! "); continue;
}
}
}
void main()
{
SeeDoctor();
}

病人的姓名等信息就在结构体里加几个成员就行了!主要功能已经实现了!

我截张图给你吧!

Ⅱ C语言,链队列问题求解队列节点间靠什么联系在一起的下面有代码请网友指点

队列是先进先出。这份code里定义了链表队列的输出结构:

typedefstruct
{
Qnode*rear;//这个rear是指向当前队列的最后一个节点
Qnode*front;//front则是指向当前队列的第一个节点。
}linkQueue;

实现队列,主要看是如何实现在队尾插入新节点,在队首删除节点的:

  1. 队尾插入新节点

intenterlinkQueue(linkQueue*Q,intx)
{
Qnode*newnode;
//这里创建了一个新节点
newnode=(Qnode*)malloc(sizeof(Qnode));
if(newnode!=NULL)
{
newnode->data=x;//为新节点赋值
newnode->next=NULL;
//令之前的队尾指向新节点,也就把新节点放在了队尾
Q->rear->next=newnode;
Q->rear=newnode;//修改原来的队尾为新节点
return1;
}
else
return0;
}

2. 队首删除节点

intdelllinkQueue(linkQueue*Q,int*x)
{
Qnode*q;
if(Q->front==Q->rear)//如果当前队列里没有元素,当然就不删除
return0;
//这个链表的实现中,front指向的节点不包含元素
//front->next才是第一个元素
//也就是下面的q,删除q,需要另front->next指向下一个元素
//也就是front->next=front->next->next;和后面的两个语句作用相同
q=Q->front->next;
Q->front->next=q->next;
if(Q->rear==q)
Q->rear=Q->front;
*x=q->data;
free(q);
return1;
}


3.补充说明,这个链表front指向的元素,不是第一个元素的原因:
(这一点从showqueue函数也能看出来)

intinitllinkqueue(linkQueue*Q)
{
//在初始化中就创建了一个新的节点,并且领fron指向该节点,但是该节点
//的data是没有赋值的,而且之后通过enterlinkQueue函数
//增加新的节点,每一次也都会malloc,所以说明第一个元素的内容为空
//front->next才是第一个元素
Q->front=(Qnode*)malloc(sizeof(Qnode));
if(Q->front!=NULL)
{
Q->rear=Q->front;
Q->front->next=NULL;
return1;
}
else
return0;

}

Ⅲ c语言队列操作

pq->rear->next
=
pnew这个代码从队列的尾部增加新节点,
然后pq->rear
=
pnew更新队列尾部指针。队列的数据结构形式就是由一个头front指针,一个尾rear指针来表征,items的设计是用空间换时间,涉及队列大小的操作会非常方便。
队列的特征是先进先出,你给出的链式实现,其实就跟一个链表一样,链表的添加删除如果能理解了,队列只是链表的元素增加/删除
按先进先出特点的一种实现。
但对于队列来说,实现方式不是重点,先进先出的性质才是重点,这在实际应用中很多,比如排队叫号。

Ⅳ 求助c语言题目。看病要排队这个是地球人都知道的常识.......(用c语言做)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<stack>
usingnamespacestd;
structq1
{
intdate1;
intdate2;
intdate3;
friendbooloperator<(q1xx,q1yy)//重载
{
if(xx.date2==yy.date2)
returnxx.date3>yy.date3;//从小到大
else
returnxx.date2<yy.date2;//从大到小
}
};
intmain()
{
intt,i,j,k,l;
charstr1[10];
while(scanf("%d",&t)!=EOF)
{
priority_queue<q1>s1;
priority_queue<q1>s2;
priority_queue<q1>s3;
q1x,y;
l=1;
for(i=0;i<t;i++)
{
scanf("%s",str1);
if(strcmp(str1,"IN")==0)
{
scanf("%d%d",&x.date1,&x.date2);
x.date3=l;
l++;
if(x.date1==1)
{
s1.push(x);
}
else
if(x.date1==2)
{
s2.push(x);
}
else
{
s3.push(x);
}
}
else
if(strcmp(str1,"OUT")==0)
{
scanf("%d",&x.date1);
if(x.date1==1)
{
if(!s1.empty())
{
y=s1.top();
s1.pop();
printf("%d ",y.date3);
}
else
printf("EMPTY ");
}
else
if(x.date1==2)
{
if(!s2.empty())
{
y=s2.top();
s2.pop();
printf("%d ",y.date3);
}
else
printf("EMPTY ");
}
else
{
if(!s3.empty())
{
y=s3.top();
s3.pop();
printf("%d ",y.date3);
}
else
printf("EMPTY ");
}
}
}
}
return0;
}

Ⅳ 求C语言实现链队列的建立和出入队列


#include<stdio.h>
#include<stdlib.h>

structNode{
intdata;//数据类型自己根据需要定义
Node*next;
};

structQueue{
Node*head,*rear;
};

Queue*creatQueue(){
Queue*Q=newQueue;
Node*node=(Node*)malloc(sizeof(Node));//创建头节点
node->next=NULL;
Q->head=Q->rear=node;
returnQ;
}

voidenQueue(Queue*Q,intd){
Node*node=(Node*)malloc(sizeof(Node));
node->data=d;
node->next=NULL;
Q->rear->next=node;
Q->rear=node;
}

intdeQueue(Queue*Q){
intdata;
if(Q->head==Q->rear){
printf("队列下溢!");
return99999999;//表示错误
}
Node*node=Q->head->next;
data=node->data;
Q->head->next=node->next;
if(node->next==NULL){
Q->rear=Q->head;
}
free(node);
returndata;
}

voidprintQueue(Queue*Q){
Node*node=Q->head->next;
puts("Queue:");
while(node!=NULL){
printf("%d",node->data);
node=node->next;
}
putchar(' ');
}

voiddestroyQueue(Queue*Q){//回收内存
Node*node;
while(Q->head){
node=Q->head->next;
free(Q->head);
Q->head=node;
}
free(Q);
}

voidmain(){
intd,run=1;
Queue*Q=creatQueue();
while(run){
system("cls");
printQueue(Q);
puts("1、入队 2、出队 3、退出 请输入操作号:");
scanf("%d",&d);
switch(d){
case1:
puts("请输入需要入队的数字:");
scanf("%d",&d);
enQueue(Q,d);
break;
case2:
printf("%d出队 ",deQueue(Q));
break;
case3:
run=0;
break;
default:
puts("输入错误! ");
}
system("pause");
}
destroyQueue(Q);
}

Ⅵ 采用链队设计一个算法,反应病人看病、排队看医生的情况,在病人排队过程当中,主要重复两件事情:

#include<stdio.h>
#include<malloc.h>
typedef struct qnode{
int data;
struct qnode *next;
}QNode; //链队结点类型
typedef struct {
QNode *front,*rear;
}QuType; //链队类型
void SeeDoctor(){ //模拟病人看病的过程
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:排队 2:就诊 3:查看排队 4:不再排队,余下依次就诊 5:下班 请选择:");
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; //将*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;
}
}
}
int main(){
SeeDoctor();
return 0;
}

Ⅶ C语言 链队列

malloc(1)改为malloc(sizeof(Queueptr));

Ⅷ C语言如何建立链队列 请教下我写的哪里出错了。

enqueue的时候,没把x赋给新结点的data域。
实在没看出来你定义一个inqueue有什么必要。

正确代码如下:
#include<stdio.h>
#include<stdlib.h>

int n=0;

#define null 0
#define len sizeof(struct queue_node)

struct queue_node
{
int data;
struct queue_node *next;
};

struct queue_node *front,*rear;

//初始化队列
void initqueue()
{
front=null;
rear=null;
}

//入队列
void enqueue(int x)
{

n++;
struct queue_node *New;
New=(struct queue_node * )malloc(len);
New->data=x;
New->next=NULL;
if(n==1)
{
front=New;
rear=New;
}
else
{
rear->next=New;
rear=New;
}
}

//出队列
void dequeue()
{
if(front==rear)
printf("队列为空\n");
else
{
struct queue_node *p=front;
front=front->next;
free(p);
}
}

//取队列的首元素
int gethead()
{
if(front==null)
printf("队列为空\n");
else
return(front->data);
}

//显示队列中的元素
void display_queue()
{
struct queue_node *p;
p=front;
printf("队列中的元素 :\n");
while(p!=null)
{
printf(" %d ",p->data);
p=p->next;
}
printf("\n");
}

//主程序
void main()
{
int k;
printf("初始化队列.\n");
initqueue();
printf("依次输入 1 2 3 4 这 4 个元素 \n");
for(k=1;k<=4;k++)
{
enqueue(k);
}
printf("显示队列中的元素\n");
display_queue();
printf("出队一次 \n");
dequeue();
printf("取队首的元素 \n");
printf("%d",gethead());
printf("显示现有的元素 \n");
display_queue();
}

Ⅸ 用c语言进行链式队列的创建,编译链接没错,但是运行的时候程序被终止。以下是源代码和注释

自行比对这两个函数吧

voidinsert_link(structlinkqueue*ps,intval)//完成队列的增加。
{
structnode*pnew=(structnode*)malloc(sizeof(structnode));//申请一个节点
pnew->data=val;//将要放入队列的值赋给节点的数据域
pnew->next=NULL;
//pnew=ps->rear->next;//将rear指向新的节点,并将新的节点的指针域置空。
ps->rear->next=pnew;
ps->rear=ps->rear->next;
//pnew->next=NULL;
}
voidtraverse_link(structlinkqueue*ps)//完成队列遍历
{
structnode*p=ps->front->next;//申请一个临时指针变量,以完成队列遍历。因为头节点没有存放数据所以让指针指向front下一个节点
while(/*p->next*/p!=NULL){//当指针指向的节点的指针域不为空时就继续下移,并且输出本节点的数据
printf("%d",p->data);
p=p->next;
}
}