當前位置:首頁 » 編程語言 » 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;
}
}