ADT

来源:互联网 发布:统计局数据由哪几部分 编辑:程序博客网 时间:2024/05/21 19:25
//ADT  Squencial Queue#define     OK                            1#define     ERROR                     0#define     TRUE                        1#define     FALSE                       0#define     OVERFLOW            -1#define     MAXQSIZE            100#define     INCREASEQSIZE    10typedef int QElemType;typedef int Status;typedef struct {    QElemType *base;    int front;//头指针    int reer;//尾指针    int length;//队列长度}SqQueue;//构造一个空队列Status InitQueue(SqQueue &Q) {    Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));    if(!Q.base) exit(OVERFLOW);    Q.front = Q.reer = 0;    Q.length = MAXQSIZE;    return OK;}//销毁队列QStatus DestroyQueue(SqQueue &Q) {    free(Q.base);    Q.front = Q.reer = 0;    Q.length = 0;    return OK;}//清空队列Status ClearQueue(SqQueue &Q) {    Q.front = Q.reer = 0;    Q.length = 0;    return OK;}//插入元素Status EnQueue(SqQueue&Q, QElemType e) {    if((Q.reer+1)%Q.length == Q.front) {        Q.base = (QElemType *)realloc(Q.base, sizeof(QElemType) * (Q.length + INCREASEQSIZE));        Q.length += INCREASEQSIZE;    }    Q.base[Q.reer] = e;    Q.reer = (Q.reer + 1) % Q.length;    return OK;}//删除元素Status DeQueue(SqQueue &Q,QElemType &e) {    if(Q.front == Q.reer) return ERROR;    e = Q.base[Q.front];    Q.front = (Q.front + 1) % Q.length;    return OK;}//判断队列是否为空Status QueueEmpty(SqQueue Q) {    if(Q.front == Q.reer) return TRUE;    return FALSE;}//求队列长度int QueueLength(SqQueue Q) {    return (Q.reer - Q.front + Q.length) % Q.length;}//求队头元素的值Status GetHead(SqQueue Q, QElemType &e) {    if(Q.front == Q.reer) return ERROR;    e = Q.base[Q.front];    return OK;}//遍历Status QueueTraverse(SqQueue Q) {    for(int i = Q.front; i != Q.reer; i = (i + 1) % Q.length) {        if(i == Q.front) cout << Q.base[i];        else cout << ' ' << Q.base[i];    }    cout << endl;}

原创粉丝点击