循环队列的基本操作
来源:互联网 发布:河北科技大学 知乎 编辑:程序博客网 时间:2024/04/27 15:51
头文件: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;
#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;
{
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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 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;
}
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;
}
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);
}
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);
}
0 0
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- 队列及循环队列的基本操作
- 循环队列基本操作
- 循环队列基本操作
- 循环队列基本操作
- 关于多线程执行显示进度条的实例
- android training管理应用的内存
- C++总结:开篇
- 安全驾驶-心病(五十五)
- java中Executor线程池讲解与实例
- 循环队列的基本操作
- spring注入方式(3)
- 安全驾驶-后视镜(五十六)
- spring中bean的配置项(4)
- mybatis多对多处理
- spring中aware接口(5)
- 安全驾驶-摩托车 (五十七)
- Hibernate Session操纵的对象状态转换图
- Service和IntentService的区别1