顺序队列

来源:互联网 发布:知乎 大国崛起纪录片 编辑:程序博客网 时间:2024/06/06 09:52
//error函数用来显示程序错误  error.c#include "error.h"void myerror(char *str){switch(errno){case -1:printf("%s:输入的参数错误\n",str);break;case FULL_ERROR:printf("%s:满队状态\n",str);break;case EMPTY_ERROR:printf("%s:空队状态\n",str);break;}}char* mystrerror(int num){switch (errno){case ERROR:return "输入参数错误";case FULL_ERROR:return "满队状态";case EMPTY_ERROR:return "空队状态";}}
//以下是error函数头文件  error.h#ifndef _ERROR_H#define _ERROR_H#include <stdio.h>#define ERROR -1#define FULL_ERROR -2#define EMPTY_ERROR -3int errno;   //错误号void myerror(char *str);char *mystrerror(int num);#endif //_ERROR_H//列队的创建与主要操作函数声明  queue.h#include <stdio.h>#include "queue.h"#ifndef _queue_h_#define _queue_h_#include "error.h"#define true 1#define false 0#define SIZE 10typedef int queuedata;typedef struct _queue{queuedata data[SIZE];int front;//指向对头下标int rear; //指向队尾下标}queue;//置空队int initqueue(queue *q) ;//进队int pushqueue (queue *q, queuedata x);  //出队int dequeue (queue *q, queuedata *x) ;//判队空否int queueempty (queue *q);//判队满否int queuefull (queue *q);//取队头int getfront(queue *q, queuedata *x);         #endif//函数源代码  queue.c#include "queue.h"int initqueue(queue *q)    //置空队{if(q == NULL){errno = ERROR;return false;}q->front = 0;q->rear  = 0;return true;}int queuefull (queue *q)   //判断列队是否是满的{if(q == NULL){errno = FULL_ERROR ;return false;}return q->front == (q->rear + 1) % SIZE;}int queueempty (queue *q)  //判断列队是否是空的{if(q == NULL){errno = EMPTY_ERROR;return false;}return q->front == q->rear;}int pushqueue (queue *q, queuedata x)  //进队{if(q == NULL){errno = ERROR;return false;}if(queuefull(q)){errno = FULL_ERROR;return false;}q->rear = (q->rear + 1) % SIZE;q->data[q->rear] = x;return true;}int dequeue (queue *q, queuedata *x)   //出队{if(q == NULL){errno = ERROR;return false;}if(queueempty(q)){errno = EMPTY_ERROR;return false;}q->front = (q->front + 1) % SIZE;*x = q->data[q->front];return true;}int getfront(queue *q, queuedata *x)   //取队头{if(q == NULL){errno = ERROR;return false;}if(queueempty(q)){errno =EMPTY_ERROR;return false;}int index;index = (q->front + 1) % SIZE;*x = q->data[index];return true;}//主函数  maic.cint main(){queue q;initqueue(&q);if(queueempty(&q)){printf("空队\n");}int i;char str[50];for(i = 0;i < 10;i++){if(pushqueue(&q,i) != true)  //进队失败{sprintf(str,"第%d个元素入队失败",i);myerror(str);}}int x;for(i = 0;i < 10;i++){if(dequeue(&q,&x) != true)  //出队失败{sprintf(str,"第%d个元素出队失败",i);myerror(str);}printf("x = %d\n",x);}return 0;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 床上有许多小虫子怎么办? 店里有许多小虫子怎么办 房间潮湿有很多小虫子怎么办 家里潮湿墙上发霉长小虫怎么办? 房间有小飞虫子怎么办 狗被灭虫剂喷了怎么办 吃鸡玩久了手机屏幕很涩怎么办 超东卧室太阳晒怎么办 床头上的布破了怎么办 老年机全静音了怎么办 老年机手机不亮怎么办 70岁老人耳朵聋怎么办 血压太低了头晕怎么办 血压高忽然变低怎么办 血压高眼睛红了怎么办 高血压200降不下去.怎么办 高血压吃药降不下来怎么办 合肥房子卖了户口怎么办 吃了粽子胃难受怎么办 突然血压高怎么办需要吃药吗? 胃一阵阵疼然后拉肚子怎么办 橱子和墙壁不平怎么办 复印选项是英文不认识怎么办 防盗门锁与门框结合不好怎么办 仿瓷涂料墙壁脏了怎么办 油笔画到墙纸上怎么办 水笔画在墙纸上怎么办 屋里有股石灰味怎么办 厨房太阳对着晒怎么办 房子有太阳西斜怎么办 房子晒到太阳很热怎么办 房子被太阳热了怎么办 房间西晒窗帘不遮光怎么办 新建房屋一面墙体有裂缝怎么办 卫生间地砖缝隙出现渗水怎么办 西户窗户太晒怎么办 西晒的墙面很烫怎么办 儿童房颜色太粉了怎么办? 小孩在家里偷钱怎么办 脾气不好的猫该怎么办 二年孩子偷钱怎么办