实验3 队列
来源:互联网 发布:卡盟顶部源码 编辑:程序博客网 时间:2024/05/21 18:34
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define MAXSIZE 6typedef int QElemType;typedef int Status;//循环队列结构定义typedef struct SqQueue{QElemType *base;int front;int rear;}SqQueue;//构造一个空队列QStatus InitQueue(SqQueue &Q){Q.base=new QElemType[MAXSIZE];if(!Q.base) return 0;Q.front=Q.rear=0;return 1;}//返回Q的元素个数,即队列的长度int QueueLength(SqQueue &Q){return(Q.rear-Q.front+MAXSIZE)%MAXSIZE;}//插入元素e为Q新的队尾元素Status EnQueue(SqQueue &Q,QElemType e){if((Q.rear+1)%MAXSIZE==Q.front) return 0;Q.base[Q.rear]=e;Q.rear=(Q.rear+1)%MAXSIZE;return 1;}//若队列不空,删除Q的队头元素,用e返回其地址Status DeQueue(SqQueue &Q,QElemType &e){if(Q.front==Q.rear) return 0;e=Q.base[Q.front];Q.front=(Q.front+1)%MAXSIZE;return e;}void PQueue(SqQueue &Q){if(Q.front==Q.rear) printf("队列为空队列\n"); else printf("队列不为空队列\n");}//输出循环队列所有元素Status QueueTraverse(SqQueue &Q){int p=Q.front;if(Q.front==Q.rear) return 0;while(p!=Q.rear){printf("%d ",Q.base[p]);p=(p+1)%MAXSIZE;}}int main(){ printf("先建立一个空的队列\n");SqQueue Q;InitQueue(Q);printf("循环队列长度=%d\n",QueueLength(Q));printf("判断是否为空队列:\n");PQueue(Q);printf("将元素1插入到队尾:\n");EnQueue(Q,1);printf("循环队列长度=%d\n",QueueLength(Q));printf("将元素2插入到队尾:\n");EnQueue(Q,2);printf("循环队列长度=%d\n",QueueLength(Q));printf("遍历输出队列的元素:\n");QueueTraverse(Q);printf("\n判断是否为空队列:\n");PQueue(Q);return 1;}
阅读全文
0 0
- 循环队列实验3
- 实验3 队列
- 数据结构_实验3:队列的应用
- php队列实验
- 栈和队列实验
- 栈和队列实验
- 栈和队列实验
- 栈与队列实验
- 循环队列实验
- 队列实验第一题
- 栈和队列实验
- 数据结构-队列实验报告
- 实验三 顺序队列
- 实验三 链队列
- 实验三、循环队列
- 实验三链队列
- 实验三顺序队列
- 实验之顺序队列
- android中getLocationInWindow 和 getLocationOnScreen的区别
- Windows10系统 DEPRECATION: The default format will switch to columns in the future解决!
- Tinyos实现温度读取汇聚收集
- linux contaner of宏
- LNMP下的Nginx的优化
- 实验3 队列
- TextView的drawable属性
- 项目整理三-Spring AOP实现日志管理
- 色彩与编码—几个小概念的综述
- centos下ifconfig没有显示eth0
- phpstudy及phpcms的安装及简单使用
- Linux系统环境变量
- Gym
- VS环境下Qt工程.UI文件不生成头文件的问题