顺序队列

顺序队列

队列的顺序存储结构
头指针指向头结点 尾指针指向尾结点的下一个位置

代码

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

#define MaxSize 10
typedef struct{
    int data[MaxSize];
    int front,rear;
}SqQueue;

//初始化
void InitQueue(SqQueue &Q){
    Q.rear = Q.front = 0;
}

//判断队空
bool QueueEmpty(SqQueue &Q){
    if (Q.rear == Q.front)
        return true;
    else
	return false;
}

//入队
bool EnQueue(SqQueue &Q, int x){
    if (Q.rear == MaxSize)
	return false;
    Q.data[Q.rear] = x;
    Q.rear = Q.rear + 1;
    return true;
}

//出队
bool DeQueue(SqQueue &Q, int &x){
    if (Q.rear == Q.front)
	return false;
    x = Q.data[Q.front];
    Q.front = Q.front + 1;
    return true;
}

//读队首元素
bool GetHead(SqQueue &Q,int &x){
    if (Q.rear == Q.front)
	return false;
    x = Q.data[Q.front];
    return true;
}

int main(){
    SqQueue Q;
    InitQueue(Q);

    int n;
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
    	int x;
    	cin >> x;
    	EnQueue(Q,x);
    }

    cout << "Q.front: "<<Q.front << " " << "Q.rear: " << Q.rear << endl;
    
    for (int i = 0; i < n - 1; i ++)
    {
    	int x;
    	DeQueue(Q,x);
    	cout << x << " ";
    }
    
    cout << endl << "Q.front: "<<Q.front << " " << "Q.rear: " << Q.rear << endl;
    //出现伪上溢出,此时队列不满
    return 0;
}
/*
4 
1 0 2 4
/*
posted @ 2021-07-26 21:14  Treasure_lee  阅读(41)  评论(0编辑  收藏  举报