数据结构题目,用c语言实现。

数据结构题目,用c语言实现。设置标志flag,当front=rear且flag=0时为队空,当front=rear且flag=1时为队满。


/* ------数据类型预定义------ */
typedef int Status;           //函数结果状态类型

/* ------函数结果状态代码------ */
#define TRUE          1
#define FALSE         0
#define OK            1
#define ERROR         0
#define OVERFLOW      -2

/* ------队列数据类型定义------ */
typedef int QElemType;        //队列中元素的数据类型

/* ------数据类型预定义------ */
typedef int Status;           //函数结果状态类型

/* ------队列动态存储分配初始常量预定义------ */
#define QUEUE_INIT_SIZE 100   //队列存储空间的初始分配量
#define QUEUEINCREMENT  10    //队列存储空间的分配增量
#define MAXQUEUESIZE    100   //循环队列最大长度

/* ------全局变量定义------ */
int FLAG;                    //出、入队列操作标志

/* ------队列存储结构类型定义------ */
typedef struct
{
QElemType *base;     //队列初始化动态分配存储空间
int front;           //对头指针向量,若队列不空,指向队列头元素
int rear;            //队尾指针向量,若队列不空,指向队列尾元素的下一个位置
}SqQueue;              //顺序队列结构类型

Status InitQueue(SqQueue &Q)
{  //构造一个空队列Q
Q.base = (QElemType *)malloc(QUEUE_INIT_SIZE * sizeof(QElemType));
if (!Q.base)
return(OVERFLOW);
Q.front = Q.rear = 0;
FLAG = 0;
return OK;
}  //InitQueue

Status StatusQueue(SqQueue &Q)
{  //返回队列当前状态
    if(Q.front == Q.rear && FLAG == 0)  //队列为空
         return FLASH;
    if(Q.front == Q.rear && FLAG == 1)  //队列为满  
         return TURE;   
}  //StatusQueue

Status EnQueue(SqQueue &Q, QElemType e)
{  //元素e入队列
if (StatusQueue(Q))              //队列为满
return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQUEUESIZE;
FLAG = 1;
        return OK;
}  //EnQueue

Status DeQueue(SqQueue &Q, QElemType &e)
{  //元素e出队列
if (!StatusQueue(Q))
return ERROR;
e = Q.base[Q.front++];
FLAG = 0;
return OK;
}  //DeQueue

温馨提示:答案为网友推荐,仅供参考
相似回答