循环队列的基本操作
2016-01-16 14:22
357 查看
头文件:SqQueue.h
#include<cstdlib>
#include<iostream>
using namespace std;
#define MAXQSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int QElemType;
typedef int Status;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
//构造一个空队列
Status InitQueue(SqQueue& Q)
{
Q.base = (QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if (!Q.base)
exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
//销毁队列Q
Status DestroyQueue(SqQueue& Q)
{
if (Q.base)
{
free(Q.base);
Q.base = NULL;
Q.front = Q.rear = 0;
}
return OK;
}
//清空队列Q
Status ClearQueue(SqQueue& Q)
{
Q.front = Q.rear = 0;
return OK;
}
//若队列Q为空,则返回TRUE;否则返回FALSE
Status QueueEmpty(SqQueue Q)
{
if (Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
//返回Q的元素个数
int QueueLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
//若队列不空,则用e返回队列Q的队头元素,并返回OK;否则返回ERROR
Status GetHead(SqQueue Q, QElemType& e)
{
if (!QueueEmpty(Q))
{
e = Q.base[Q.front];
return OK;
}
return ERROR;
}
//插入元素e为Q新的队尾元素
Status EnQueue(SqQueue& Q, QElemType e)
{
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
Status DeQueue(SqQueue& Q, QElemType& e)
{
if (Q.front == Q.rear)
return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
return OK;
}
//打印输出队列的元素
Status visit(QElemType data)
{
if (cout << data << " ")
return OK;
else
return ERROR;
}
//依次对队列Q的元素调用函数visit().一旦调用visit()失败,则操作失败
Status QueueTraverse(SqQueue Q, Status(*visit)(QElemType))
{
int k = Q.front;
for (int i = 1; i <= QueueLength(Q); ++i)
{
if (!(*visit)(Q.base[k]))
return ERROR;
k = (k + 1) % MAXQSIZE;
}
cout << endl;
return OK;
}
主程序:
#include"SqQueue.h"
void main(void)
{
SqQueue Q;
InitQueue(Q);
for (int i = 1; i < 20; i += 2)
EnQueue(Q, i);
cout << "队列Q的基本信息:" << endl;
QueueTraverse(Q, visit);
cout << "Q的长度为:" << QueueLength(Q) << endl;
cout << "Q的队头元素为:";
QElemType e;
GetHead(Q, e);
cout << e << endl;
cout << "Q是否为空:";
if (!QueueEmpty(Q))
{
cout << "否!" << endl;
int i = 1;
while (i <= QueueLength(Q))
{
DeQueue(Q, e);
++i;
}
}
else
cout << "是!";
DestroyQueue(Q);
}
#include<cstdlib>
#include<iostream>
using namespace std;
#define MAXQSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int QElemType;
typedef int Status;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
//构造一个空队列
Status InitQueue(SqQueue& Q)
{
Q.base = (QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if (!Q.base)
exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
//销毁队列Q
Status DestroyQueue(SqQueue& Q)
{
if (Q.base)
{
free(Q.base);
Q.base = NULL;
Q.front = Q.rear = 0;
}
return OK;
}
//清空队列Q
Status ClearQueue(SqQueue& Q)
{
Q.front = Q.rear = 0;
return OK;
}
//若队列Q为空,则返回TRUE;否则返回FALSE
Status QueueEmpty(SqQueue Q)
{
if (Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
//返回Q的元素个数
int QueueLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
//若队列不空,则用e返回队列Q的队头元素,并返回OK;否则返回ERROR
Status GetHead(SqQueue Q, QElemType& e)
{
if (!QueueEmpty(Q))
{
e = Q.base[Q.front];
return OK;
}
return ERROR;
}
//插入元素e为Q新的队尾元素
Status EnQueue(SqQueue& Q, QElemType e)
{
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
Status DeQueue(SqQueue& Q, QElemType& e)
{
if (Q.front == Q.rear)
return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
return OK;
}
//打印输出队列的元素
Status visit(QElemType data)
{
if (cout << data << " ")
return OK;
else
return ERROR;
}
//依次对队列Q的元素调用函数visit().一旦调用visit()失败,则操作失败
Status QueueTraverse(SqQueue Q, Status(*visit)(QElemType))
{
int k = Q.front;
for (int i = 1; i <= QueueLength(Q); ++i)
{
if (!(*visit)(Q.base[k]))
return ERROR;
k = (k + 1) % MAXQSIZE;
}
cout << endl;
return OK;
}
主程序:
#include"SqQueue.h"
void main(void)
{
SqQueue Q;
InitQueue(Q);
for (int i = 1; i < 20; i += 2)
EnQueue(Q, i);
cout << "队列Q的基本信息:" << endl;
QueueTraverse(Q, visit);
cout << "Q的长度为:" << QueueLength(Q) << endl;
cout << "Q的队头元素为:";
QElemType e;
GetHead(Q, e);
cout << e << endl;
cout << "Q是否为空:";
if (!QueueEmpty(Q))
{
cout << "否!" << endl;
int i = 1;
while (i <= QueueLength(Q))
{
DeQueue(Q, e);
++i;
}
}
else
cout << "是!";
DestroyQueue(Q);
}
相关文章推荐
- Delphi中BCD和Currency类型
- Web前端学习①Web基础知识
- C++总结:开篇
- android training管理应用的内存
- Delphi XE4 inline 的用法
- Delphi:Indy9的IdFTP完全使用
- MFC学习的笔记
- 关于多线程执行显示进度条的实例
- DELPHI IDFTP
- lock与C#多线程
- Zookeeper体系结构
- maven 工程启动找不到 Spring ContextLoaderListener 的解决办法
- 【51单片机】数据管的消影问题
- 下拉刷新 上拉分页 滑动删除listview
- Delphi 快捷键
- 今天尝试了下COMSOL计算热电耦合,
- android query方法各个参数的含义
- 将HDFS中的数据通过MapReduce产生HFile,然后将HFile导入到HBase具体案例分析
- List、Set、 数组等转字符串 (均通过str = StringUtils.join(set.toArray(), ",");)
- SpringMVC拦截器的实现