1. 队列的定义与操作
1.1 队列的定义
插入(入队)在一端(末尾)进行而删除(出列)在另一端(队首)进行的线性表。即Fist in First out:先进先出的线性表
线性表:a0,a1,a2,a3…an的入队与出队
1.2 队列的基本操作
- 入队操作:将数据元素插入队尾
- 出队操作:移除队首的数据元素
- 是否为空:判断队中实际包含数据元素的个数
- 清空操作:移除队中的所有数据元素
- 清空操作:移除队列中的所有数据元素
- 获取队首元素:
2. 队列的表示与实现
2.1 顺序存储实现
- 循环队列(顺序存储):利用数组采用循环的方式实现队列,还必须要附设两个指针front和rear分别指向队列头元素和队列尾元素的位置。
- 形象演示
- 链队列(链存储):利用单链表实现的队列;
3. 练习
1. 模拟银行服务完成程序代码。
目前,在以银行营业大厅为代表的窗口行业中大量使用排队(叫号)系统,该系统完全模拟了人群排队全过程,通过取票进队、排队等待、叫号服务等功能,代替了人们站队的辛苦。
排队叫号软件的具体操作流程为:
-
顾客取服务序号
当顾客抵达服务大厅时,前往放置在入口处旁的取号机,并按一下其上的相应服务按钮,取号机会自动打印出一张服务单。单上显示服务号及该服务号前面正在等待服务的人数。 -
服务员工呼叫顾客
服务员工只需按一下其柜台上呼叫器的相应按钮,则顾客的服务号就会按顺序的显示在显示屏上,并发出“叮咚”和相关语音信息,提示顾客前往该窗口办事。当一位顾客办事完毕后,柜台服务员工只需按呼叫器相应键,即可自动呼叫下一位顾客。
编写程序模拟上面的工作过程,主要要求如下:
程序运行后,当看到“请点击触摸屏获取号码:”的提示时,只要按回车键,即可显示“您的号码是:XXX,您前面有YYY位”的提示,其中XXX是所获得的服务号码,YYY是在XXX之前来到的正在等待服务的人数。
用多线程技术模拟服务窗口(可模拟多个),具有服务员呼叫顾客的行为,假设每个顾客服务的时间是10000ms,时间到后,显示“请XXX号到ZZZ号窗口!”的提示。其中ZZZ是即将为客户服务的窗口号。
2.代码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
#include <time.h>
int g_number = 0;
int add_queue(SEQQUEUE *queue)
{
int ret = 0;
int number = 0;
DATATYPE_T data;
ret = queue_full(queue);
if(ret==0)
{
data.number = ++g_number;
data.time = time(NULL);
queue_push(queue,data);
}
else
{
printf("line up too much,please waiting\n");
return -1;
}
return 0;
}
int next(SEQQUEUE *queue)
{
int ret = 0;
DATATYPE_T *data;
ret = queue_empty(queue);
if(ret)
{
printf("the queue is empty\n");
return -1;
}
data = queue_pop(queue);
printf("0x%x\n",data);
printf("please number is %d handles the service\n",data->number);
}
void menu()
{
printf("********************\n");
printf("1.new consumer\n");
printf("2.next consumer\n");
printf("0.quit ");
printf("\n");
printf("********************\n");
}
int main()
{
int opt = 0;
int len = 0;
DATATYPE_T data;
SEQQUEUE *queue=NULL;
queue = queue_init();
if(queue==NULL)
{
printf("creat queue failed\n");
return -1;
}
do
{
menu();
printf("please input select!\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
add_queue(queue);
len = get_queue_length(queue);
printf("current wait for %d consumer\n",len);
break;
case 2:
next(queue);
len = get_queue_length(queue);
printf("current wait for %d consumer\n",len);
break;
case 0:
break;
default:
break;
}
}while(opt!=0);
return 0;
}
头文件
#ifndef _SEQQUEUE_H_
#define _SEQQUEUE_H_
#define QUEUE_MAX 15 // 宏定义
// 队列结构实现
typedef struct
{
int number; //顾客编号
long time;//进入时间
}DATATYPE_T;
typedef struct
{
DATATYPE_T data[QUEUE_MAX]; //队列数组
int head; //队列头
int tail; //队列尾
}SEQQUEUE;
/*队列是否为空*/
int queue_empty(SEQQUEUE *queue);
/*队列是否满*/
int queue_full(SEQQUEUE *queue);
/*队列初始化*/
SEQQUEUE *queue_init();
/*进队列*/
int queue_push(SEQQUEUE *queue,DATATYPE_T data);
/*出队列*/
DATATYPE_T *queue_pop(SEQQUEUE *queue);
/* 获取队列的第一个元素*/
DATATYPE_T *get_queue_first_element(SEQQUEUE *queue);
/*获取队列的长度*/
int get_queue_length(SEQQUEUE *queue);
/*释放队列*/
void queue_free(SEQQUEUE *queue);
/*显示队列信息*/
int show_queue_info(SEQQUEUE *queue);
#endif
//函数实现
/*队列是否为空*/
int queue_empty(SEQQUEUE *queue)
{
return (queue->head==queue->tail);
}
/*队列是否满*/
int queue_full(SEQQUEUE *queue)
{
return (queue->tail==QUEUE_MAX);
}
/*队列初始化*/
SEQQUEUE *queue_init()
{
SEQQUEUE *queue=NULL;
queue = (SEQQUEUE*)malloc(sizeof(SEQQUEUE));
if(queue == NULL)
{
printf("malloc failed\n");
return NULL;
}
queue->head = 0;
queue->tail = 0;
return queue;
}
/*进队列*/
int queue_push(SEQQUEUE *queue,DATATYPE_T data)
{
int ret = 0;
ret = queue_full(queue);
if(ret!=0)
{
printf("the queue is full\n");
return -1;
}
queue->tail++;
queue->data[queue->tail] = data;
return 0;
}
/*出队列*/
DATATYPE_T *queue_pop(SEQQUEUE *queue)
{
int ret = 0;
ret = queue_empty(queue);
if(ret!=0)
{
printf("the queue is empty\n");
return NULL;
}
queue->head++;
return &(queue->data[queue->head]);
}
/* 获取队列的第一个元素*/
DATATYPE_T *get_queue_first_element(SEQQUEUE *queue)
{
int ret = 0;
ret = queue_empty(queue);
if(ret!=0)
{
printf("the queue is empty\n");
return NULL;
}
return &(queue->data[queue->head]);
}
/*获取队列的长度*/
int get_queue_length(SEQQUEUE *queue)
{
return ((queue->tail)-(queue->head));
}
/*释放队列*/
void queue_free(SEQQUEUE *queue)
{
if(queue!=NULL)
{
free(queue);
queue = NULL;
}
}
/*显示队列全部信息 */
int show_queue_info(SEQQUEUE *queue)
{
int i = 0;
for(i=queue->head+1;i<(queue->tail+1);i++)
{
printf("num:%d,time:%d\n",queue->data[i].number,queue->data[i].time);
}
return 0;
}