队列

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

typedef  int  ElemType;

class SeqQueue {  //顺序栈类定义

protected:

    int rear, front;
    ElemType *elements; //数组存放栈元素

    int maxSize;               //栈最大容量

public:

    SeqQueue(int sz);                    //构造函数

    ~SeqQueue() { delete[]elements; };        //析构函数

    bool Enqueue(ElemType x);    //入队

    bool DeQueue(ElemType &x);     //出队

    bool getFront(ElemType &x);

    int IsEmpty() const { return rear == front; }

    int IsFull() const { return (rear + 1) % maxSize == front; }

    int GetSize() const { return (rear - front + maxSize) % maxSize; }

};

SeqQueue::SeqQueue(int sz) {
    front=rear=0;
    maxSize=sz;
    elements=new ElemType [sz];
    if (elements==NULL) {
        cout << "空间申请错误!" << endl;
        exit(1);
    }
}
bool SeqQueue::Enqueue(ElemType x) {
    if (IsFull() == 1) return 0;

    elements[rear] = x;
    rear = (rear + 1) % maxSize;
    return 1;
}
bool SeqQueue::DeQueue(ElemType &x) {
    if (IsEmpty()) return 0;
    x = elements[front];
    front = (front + 1) % maxSize;
    return 1;
}

int main() {
    int n;
    SeqQueue *result = new SeqQueue(1050);
    cin >> n;
    for (int i = 1, x; i <= n; i++) {
        cin >> x;
        result->Enqueue(x);
    }
    while (result->IsEmpty() == 0) {
        int x;
        result->DeQueue(x);
        cout << x << endl;
    }
}
posted @ 2020-06-10 16:27  Snow_in_winer  阅读(107)  评论(0编辑  收藏  举报