补充完整的代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int QElemType;
#define MAX_QSIZE 5 // 最大队列长度+1
struct SqQueue
{
QElemType *base; // 初始化的动态分配存储空间
int front; // 头指针,若队列不空,指向队列头元素
int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
};
void print(QElemType i)
{
printf("%d ",i);
}
void InitQueue(SqQueue &Q); // 构造一个空队列Q
void DestroyQueue(SqQueue &Q); // 销毁队列Q,Q不再存在
void ClearQueue(SqQueue &Q); // 将Q清为空队列
int QueueEmpty(SqQueue Q); // 若队列Q为空队列,则返回TRUE;否则返回FALSE
int QueueLength(SqQueue Q); // 返回Q的元素个数,即队列的长度
int GetHead(SqQueue Q, QElemType &e); // 若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR
int EnQueue(SqQueue &Q, QElemType e); // 插入元素e为Q的新的队尾元素
int DeQueue(SqQueue &Q, QElemType &e); // 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
void QueueTraverse(SqQueue Q, void(*vi)(QElemType)); // 从队头到队尾依次对队列Q中每个元素调用函数vi()
int main()
{
int j;
int i=0,l;
QElemType d;
SqQueue Q;
InitQueue(Q);
for(i=0; i<MAX_QSIZE; i++)
{
scanf("%d", &d);
EnQueue(Q, d);
};
printf("队列长度为: %d\n", QueueLength(Q));
printf("现在队列中元素:\n");
QueueTraverse(Q, print);
DeQueue(Q, d);
printf("删除的元素是%d\n", d);
DeQueue(Q, d);
printf("删除的元素是%d\n", d);
scanf("%d", &d);
EnQueue(Q, d);
printf("队列长度为: %d\n", QueueLength(Q));
printf("现在队列中元素:\n");
QueueTraverse(Q, print);
j = GetHead(Q, d);
if(j)
printf("现在队头元素为:%d\n", d);
ClearQueue(Q);
DestroyQueue(Q);
return 0;
}
// 循环队列的基本操作(9个)
void InitQueue(SqQueue &Q)
{ // 构造一个空队列Q
/********** Begin **********/
Q.base = (QElemType*)malloc(sizeof(QElemType) * MAX_QSIZE); // 动态分配存储空间
if(!Q.base) exit(OVERFLOW);
Q.front = Q.rear = 0; // 初始化队头和队尾指针为0
/********** End **********/
}
void DestroyQueue(SqQueue &Q)
{ // 销毁队列Q,Q不再存在
/********** Begin **********/
free(Q.base); // 释放动态分配的存储空间
Q.base = NULL;
Q.front = Q.rear = 0; // 队头和队尾指针置零
/********** End **********/
}
void ClearQueue(SqQueue &Q)
{ // 将Q清为空队列
/********** Begin **********/
Q.front = Q.rear = 0; // 队头和队尾指针置零即可实现清空操作
/********** End **********/
}
int QueueEmpty(SqQueue Q)
{ // 若队列Q为空队列,则返回TRUE;否则返回FALSE
/********** Begin **********/
if(Q.front == Q.rear) return TRUE;
else return FALSE;
/********** End **********/
}
int QueueLength(SqQueue Q)
{ // 返回Q的元素个数,即队列的长度
/********** Begin **********/
return (Q.rear - Q.front + MAX_QSIZE) % MAX_QSIZE;
/********** End **********/
}
int GetHead(SqQueue Q, QElemType &e)
{ // 若队列不空,则用e返回Q的对头元素,并返回OK;否则返回ERROR
/********** Begin **********/
if(QueueEmpty(Q)) return ERROR;
e = Q.base[Q.front];
return OK;
/********** End **********/
}
int EnQueue(SqQueue &Q, QElemType e)
{ // 插入元素e为Q的新的队尾元素
/********** Begin **********/
if((Q.rear + 1) % MAX_QSIZE == Q.front) return ERROR; // 队满,无法插入
Q.base[Q.rear] = e; // 将新元素放入队尾
Q.rear = (Q.rear + 1) % MAX_QSIZE; // 队尾指针后移一位
return OK;
/********** End **********/
}
int DeQueue(SqQueue &Q, QElemType &e)
{ // 若队列不空,则删除Q的对头元素,用e返回其值,并返回OK;否则返回ERROR
/********** Begin **********/
if(QueueEmpty(Q)) return ERROR; // 队空,无法删除
e = Q.base[Q.front]; // 将队头元素赋值给e
Q.front = (Q.front + 1) % MAX_QSIZE; // 队头指针后移一位
return OK;
/********** End **********/
}
void QueueTraverse(SqQueue Q, void(*vi)(QElemType))
{ // 从队头到队尾依次对队列Q中每个元素调用函数vi()
int i;
i = Q.front;
while(i != Q.rear)
{
vi(Q.base[i]);
i = (i + 1) % MAX_QSIZE;
}
}
这是一个简单的循环队列的实现,包含了初始化队列、销毁队列、清空队列、判断队列是否为空、获取队头元素、入队操作、出队操作以及遍历队列等基本操作。你可以根据需要修改代码,并进行测试运行。
内容由零声教学AI助手提供,问题来源于学员提问