顺序队列
来源:互联网 发布:知乎 大国崛起纪录片 编辑:程序博客网 时间: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;}
阅读全文
0 0
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- 顺序队列
- android_解析json
- Java多线程【三】
- 解决IDEA的"Cannot run program "git.exe": CreateProcess error=2, 系统找不到指定的文件"错误
- Java和Scala学习日记1
- UNIX环境高级编程读书笔记(1)
- 顺序队列
- Unity 之MVC框架 StrangeIoC
- QPBOC电子现金与标准借贷记的脱机认证详细过程
- AVL的详细讲解
- VS2010 MFC:“Debug Assertion Failed!” ——自动生成的单文档程序项目编译运行就有错
- CSS3选择器和相关属性
- JAVA中的反射机制
- Jvm对象探秘
- 再来