class Queue { private: int* arr; int front; int rear; int capacity; public: Queue(int size) { arr = new int[size]; capacity = size; front = 0; rear = -1; } ~Queue() { delete[] arr; } void enqueue(int val) { if(isFull()) { cout << 'Queue is full.' << endl; return; } rear++; arr[rear] = val; } int dequeue() { if(isEmpty()) { cout << 'Queue is empty.' << endl; return -1; } int val = arr[front]; front++; return val; } bool isEmpty() { return (rear < front); } bool isFull() { return (rear == capacity - 1); } };

C++ 队列类实现:enqueue、dequeue、isEmpty、isFull

原文地址: https://www.cveoy.top/t/topic/oX1a 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录