91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

C++循環隊列的實現方法

c++
小樊
82
2024-07-14 10:11:27
欄目: 編程語言

#include <iostream>

#define MAX_SIZE 10

class CircularQueue {
private:
    int front, rear;
    int queue[MAX_SIZE];

public:
    CircularQueue() {
        front = -1;
        rear = -1;
    }

    bool isEmpty() {
        return front == -1 && rear == -1;
    }

    bool isFull() {
        return (rear + 1) % MAX_SIZE == front;
    }

    void enqueue(int value) {
        if (isFull()) {
            std::cout << "Queue is full. Cannot enqueue.\n";
            return;
        }

        if (isEmpty()) {
            front = 0;
            rear = 0;
        } else {
            rear = (rear + 1) % MAX_SIZE;
        }

        queue[rear] = value;
        std::cout << value << " enqueued to the queue.\n";
    }

    void dequeue() {
        if (isEmpty()) {
            std::cout << "Queue is empty. Cannot dequeue.\n";
            return;
        }

        std::cout << queue[front] << " dequeued from the queue.\n";

        if (front == rear) {
            front = -1;
            rear = -1;
        } else {
            front = (front + 1) % MAX_SIZE;
        }
    }

    void display() {
        if (isEmpty()) {
            std::cout << "Queue is empty.\n";
            return;
        }

        int i = front;
        while (i != rear) {
            std::cout << queue[i] << " ";
            i = (i + 1) % MAX_SIZE;
        }
        std::cout << queue[rear] << "\n";
    }
};

int main() {
    CircularQueue q;

    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    q.display();

    q.dequeue();
    q.display();

    q.enqueue(40);
    q.display();

    return 0;
}

0
惠东县| 长白| 板桥市| 鹿邑县| 平昌县| 丽江市| 长阳| 丁青县| 阜城县| 温宿县| 莒南县| 和林格尔县| 安陆市| 专栏| 义马市| 宜君县| 永济市| 郎溪县| 宝兴县| 贵州省| 顺义区| 东丽区| 沈阳市| 土默特左旗| 淮安市| 平和县| 平谷区| 敦煌市| 宣威市| 西和县| 崇仁县| 乐亭县| 丹凤县| 漳平市| 都兰县| 新化县| 黑山县| 阿合奇县| 博罗县| 历史| 佛山市|