线性表

栈&队列

二叉树

持续更新中,尽请期待!!!

循环队列

#include<bits/stdc++.h>
using namespace std;
#define QElemType int
#define Status int
#define MAXQSIZE 10
//----------循环队列------------
typedef struct{
   
    QElemType *base;
    int front;
    int rear;
} SqQueue;
//----------函数申明------------
Status InitQueue(SqQueue &Q);
Status DestroyQueue(SqQueue &Q);
Status ClearQueue(SqQueue &Q);
Status QueueEmpty(SqQueue Q);
Status QueueLength(SqQueue Q);
Status GetHead(SqQueue &Q, QElemType &e);
Status EnQueue(SqQueue &Q, QElemType e);
Status DeQueue(SqQueue &Q, QElemType &e);
void visit(QElemType a);
Status QueueTraverse(SqQueue &Q, void (*visit)(QElemType));
//----------基本操作------------
Status InitQueue(SqQueue &Q) {
   
    Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
    if (!Q.base)
        exit(_OVERFLOW);
    Q.front = Q.rear = 0;
    return 1;
}

Status DestroyQueue(SqQueue &Q){
   
    while(!QueueEmpty(Q)){
   
        free(&Q.base[Q.front]);
        Q.front = (Q.front + 1) % MAXQSIZE;
    }
}

Status ClearQueue(SqQueue &Q){
   
    if(QueueEmpty(Q))
        return 0;
    QElemType i = QueueLength(Q);
    while(i--){
   
        Q.base[Q.front] = 0;
        Q.front = (Q.front + 1) % MAXQSIZE;
    }
    Q.front = Q.rear = 0;
    return 1;
}

Status QueueEmpty(SqQueue Q){
   
	if(Q.front==Q.rear)
        return 1;
    else
        return 0;
}

Status QueueLength(SqQueue Q){
   
    return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}


Status GetHead(SqQueue &Q,QElemType &e)
{
   
	if(Q.front==Q.rear)
        return 0;
    e=Q.base[Q.front];
	return 1;
}

Status EnQueue(SqQueue &Q,QElemType e){
   
    if ((Q.rear + 1) % MAXQSIZE == Q.front)
        return 0;
    Q.base[Q.rear]=e;
    Q.rear = (Q.rear + 1) % MAXQSIZE;
    return 1; 
}

Status DeQueue(SqQueue &Q,QElemType &e){
   
	if(Q.front==Q.rear)
        return 0;
    e=Q.base[Q.front];					
	Q.front=(Q.front+1)%MAXQSIZE;		
	return 1;
}

void visit(QElemType a){
   
    cout << a << " ";
}
Status QueueTraverse(SqQueue &Q,void (*visit)(QElemType)){
   
    if(QueueEmpty(Q)){
   
        cout << "The queue is empty!\n";
    }
    else{
   
        for (int i = Q.front; i != Q.rear; i = (i + 1) % MAXQSIZE)
            visit(Q.base[i]);
        cout << endl;
    }
    return 1;
}

int main(){
   
    SqQueue Q;
    QElemType e;
    InitQueue(Q);
    cout << "是否为空:" << QueueEmpty(Q) << endl;
    for (int i = 0; i < 8;i++){
   
        EnQueue(Q, i);
    }
    cout << "队列元素的个数:" << QueueLength(Q)<<endl;
    QueueTraverse(Q, visit);
    cout << "出对的元素为:";
    for (int i = 0; i < 4;i++){
   
        DeQueue(Q, e);
        cout << e << " ";
    }
    cout << endl;
    cout << "是否为空:" << QueueEmpty(Q) << endl;
    cout << "队列元素的个数:" << QueueLength(Q)<<endl;
    for (int i = 8; i < 12;i++){
   
        EnQueue(Q, i);
    }
    cout << "队列元素的个数:" << QueueLength(Q)<<endl;
    QueueTraverse(Q, visit);
    GetHead(Q, e);
    cout << "输出队头:" << e << endl;
    ClearQueue(Q);
    cout << "队列元素的个数:" << QueueLength(Q)<<endl;
    cout << "是否为空:" << QueueEmpty(Q) << endl;

}

测试结果: