8.用定长数组 实现 循环队列

来源:互联网 发布:js 数组排序函数 编辑:程序博客网 时间:2024/05/21 22:29
// 用定长数组 实现 循环队列#include<stdio.h>#include<stdlib.h>#define MAXSIZE 100typedef bool Status;#define OK 0#define ERROR 1typedef int QElemType;typedef struct QNode{QElemType *base;int front;int rear;}SqQueue;Status InitQueue(SqQueue &Q){Q.base = (QElemType *)malloc(sizeof(QElemType)*MAXSIZE);if (!Q.base) return ERROR;Q.front = 0;Q.rear = 0;return OK;}Status QueueLengh(SqQueue &Q, int &L){L = (MAXSIZE + Q.front - Q.rear) % MAXSIZE;return OK;}Status EnQueue(SqQueue &Q, QElemType e){if ((Q.rear + 1) % MAXSIZE == Q.front){return ERROR;}//满Q.base[Q.rear] = e;Q.rear = (Q.rear + 1) % MAXSIZE;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) % MAXSIZE;return OK;}int main(){//我就不写测试程序了....return 0;}

0 0
原创粉丝点击