2.2 先进先出结构:队列
2016-07-18 23:27
260 查看
2-8SeqQueue.h
2-9 CycQueue.h
2-10 BankQueue.c
//顺序队列操作 #define QUEUEMAX 15 typedef struct { DATA data[QUEUEMAX]; //队列数组 int head; //队头 int tail; //队尾 }SeqQueue; SeqQueue *SeqQueueInit() { SeqQueue *q; if(q=(SeqQueue *)malloc(sizeof(SeqQueue))) //申请保存队列的内存 { q->head = 0;//设置队头 q->tail = 0;//设置队尾 return q; }else return NULL; //返回空 } void SeqQueueFree(SeqQueue *q) //释放队列 { if (q!=NULL) free(q); } int SeqQueueIsEmpty(SeqQueue *q) //队列是否为空 { return (q->head==q->tail); } int SeqQueueIsFull(SeqQueue *q)//队列是否已满 { return (q->tail==QUEUEMAX); } int SeqQueueLen(SeqQueue *q) //获取队列长度 { return(q->tail-q->head); } int SeqQueueIn(SeqQueue *q,DATA data)//顺序队列的入队函数 { if(q->tail==QUEUEMAX) { printf("队列已满!\n"); return(0); }else{ q->data[q->tail++]=data; return(1); } } DATA *SeqQueueOut(SeqQueue *q)//顺序队列的出队 { if(q->head==q->tail) { printf("\n队列已空!\n"); return NULL; }else{ return &(q->data[q->head++]); } } DATA *SeqQueuePeek(SeqQueue *q) //获取队头元素 { if(SeqQueueIsEmpty(q)) { printf("\n队列为空!\n"); return NULL; }else{ return &(q->data[q->head]); } }
2-9 CycQueue.h
//循环队列 #define QUEUEMAX 15 typedef struct { DATA data[QUEUEMAX]; //队列数组 int head; //队头 int tail; //队尾 }CycQueue; CycQueue *CycQueueInit() { CycQueue *q; if(q=(CycQueue *)malloc(sizeof(CycQueue))) //申请保存队列的内存 { q->head = 0;//设置队头 q->tail = 0;//设置队尾 return q; }else return NULL; //返回空 } void CycQueueFree(CycQueue *q) //释放队列 { if (q!=NULL) free(q); } int CycQueueIsEmpty(CycQueue *q) //队列是否为空 { return (q->head==q->tail); } int CycQueueIsFull(CycQueue *q)//队列是否已满 { return ((q->tail+1)%QUEUEMAX==q->head); } int CycQueueIn(CycQueue *q,DATA data)//入队函数 { if((q->tail+1)%QUEUEMAX == q->head ) { printf("队列已满!\n"); return 0; }else{ q->tail=(q->tail+1)%QUEUEMAX;//求列尾序号 q->data[q->tail]=data; return 1; } } DATA *CycQueueOut(CycQueue *q)//循环队列的出队函数 { if(q->head==q->tail) //队列为空 { printf("队列已空!\n"); return NULL; }else{ q->head=(q->head+1)%QUEUEMAX; return &(q->data[q->head]); } } int CycQueueLen(CycQueue *q) //获取队列长度 { int n; n=q->tail-q->head; if(n<0) n=QUEUEMAX+n; return n; } DATA *CycQueuePeek(CycQueue *q) //获取队定中第1个位置的数据 { if(q->head==q->tail) { printf("队列已经为空!\n"); return NULL; }else{ return &(q->data[(q->head+1)%QUEUEMAX]); } }
2-10 BankQueue.c
#include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct { int num; //顾客编号 long time;//进入队列时间 }DATA; #include "2-9 CycQueue.h" int num;//顾客序号 void add(CycQueue *q) //新增顾客排列 { DATA data; if(!CycQueueIsFull(q)) //如果队列未满 { data.num=++num; data.time=time(NULL); CycQueueIn(q,data); } else printf("\n排队的人太多,请稍候再排队!\n"); } void next(CycQueue *q) //通知下一顾客准备 { DATA *data; if(!CycQueueIsEmpty(q)) //若队列不为空 { data=CycQueueOut(q); //取队列头部的数据 printf("\n请编号为%d的顾客办理业务!\n",data->num); } if(!CycQueueIsEmpty(q)) //若队列不为空 { data=CycQueuePeek(q);//取队列中指定位置的数据 printf("请编号为%d的顾客准备,马上将为您理业务!\n",data->num); } } int main() { CycQueue *queue1; int i,n; char select; num=0;//顾客序号 queue1=CycQueueInit(); //初始化队列 if(queue1==NULL) { printf("创建队列时出错!\n"); getch(); return 0; } do{ printf("\n请选择具体操作:\n"); printf("1.新到顾客\n"); printf("2.下一个顾客\n"); printf("0.退出\n") ; fflush(stdin); select=getch(); switch(select) { case '1': add(queue1); printf("\n现在共有%d位顾客在等候!\n",CycQueueLen(queue1)); break; case '2': next(queue1); printf("\n现在共有%d位顾客在等候!\n",CycQueueLen(queue1)); break; case '0': break; } }while(select!='0'); CycQueueFree(queue1); //释放队列 getch(); return 0; }
相关文章推荐
- win7 64位安装redis 及Redis Desktop Manager使用
- 在Java中如何高效的判断数组中是否包含某个元素
- ASP.NET Core 和 NLog 集成
- leetcode-Intersection of Two Arrays-349
- strcmp()和strncmp()函数的实现
- 如何删除计算机中的服务
- mfc学习笔记之如何修改mfc消息机制处理顺序
- 搭建基于SSH的Git服务器
- 心理学的入门
- oracle11g 拆分字符串的详细技巧
- 线程池
- 引用js文件
- Java面试参考指南——同步
- Android .gitignore
- A Neural Probabilistic Language Model
- 剑指offer——对称的二叉树判断
- #Android#集合与异常处理
- 【在线笔试题解题报告系列】微软在线笔试之 2016微软探星夏令营在线技术笔试(时间:2016.07.17)
- 2.1顺序表(链表)
- Python之queue模块以及生产消费者模型