用链表实现该队列,相比于之前的队列,这个队列只是增加了容量而已。

#include <iostream>
#include <string>
using namespace std;
class node {
  public:
    int x;
    node* pnext = nullptr;
    node(int x) {
        this->x = x;
    }
};
class queue {
  private:
    int size;
    int k;
    node* pfront; // 队首指针
    node* prear; // 队尾指针
  public:
    queue(int size) {
        this->size = size;
        this->k = 0;
        pfront = nullptr;
        prear = nullptr;
    }
    void push(int x) {
        if (k == size)
            cout << "full" << endl;
        else {
            k += 1;
            node* tmp = new node(x);
            if (pfront == nullptr) {
                pfront = tmp;
                prear = tmp;
            } else {
                prear->pnext = tmp;
                prear = tmp;
            }
        }

    }
    void pop() {
        if (k == 0)
            cout << "empty" << endl;
        else {
            k -= 1;
            cout << pfront->x << endl;
            if (pfront == prear) {
                free(pfront);
                pfront = nullptr;
                prear = nullptr;
            } else {
                node* tmp = pfront;
                pfront = pfront->pnext;
                free(tmp);
            }
        }
    }
    void front() {
        if (k == 0)
            cout << "empty" << endl;
        else
            cout << pfront->x << endl;
    }
};

int main() {
    int size, n;
    cin >> size >> n;
    queue q(size);
    for (int i = 0; i < n; i++) {
        string op;
        cin >> op;
        if (op == "push") {
            int x;
            cin >> x;
            q.push(x);
        } else if (op == "pop") {
            q.pop();
        } else if (op == "front") {
            q.front();
        }
    }
    return 0;
}