您的位置:首页 > 理论基础 > 数据结构算法

数据结构五—循环队列的顺序存储结构

2013-02-25 22:44 495 查看
#include <iostream>

using namespace std;
#define MAXSIZE 100
#define ERROR 0
#define OK 0
typedef int Status;
typedef int QElemType;

typedef struct
{
QElemType data[MAXSIZE];
int front;
int rear;
}SqQueue;

Status InitQueue(SqQueue* Q)
{
Q->front = 0;
Q->rear = 0;
return OK;
}

int QueueLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

Status EndQueue(SqQueue* Q, QElemType e)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)
{
return ERROR;
}

Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % MAXSIZE;
return OK;
}

Status DeQueue(SqQueue* Q, QElemType* e)
{
if (Q->rear == Q->front)
{
return ERROR;
}

*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return OK;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: