循环队列(顺序与链式)

链式队列:

#include <iostream>

using namespace std; //这是应该带头结点的链式队列

// 定义链表节点结构体
struct Node {
    int data; // 数据
    Node* next; // 指向下一个节点的指针
};

class LinkQueue {
private:
    Node* head; // 头结点指针
    Node* tail; // 尾结节指针
    int length; // 队列长度

public:
    // 构造函数
    LinkQueue() {
        head = new Node; // 创建头结点
        tail = head; // 初始时尾结点也指向头结点
        length = 0; // 初始化队列长度
        head->next = nullptr; // 头结点的下一个节点为空
    }

    // 析构函数
    ~LinkQueue() {
        while (!isEmpty()) { // 清空队列
            deQueue(nullptr);
        }
        delete head; // 删除头结点
    }

    // 入队操作
    void enQueue(int x) {
        Node* newNode = new Node;
        newNode->data = x;
        newNode->next = nullptr;

        tail->next = newNode; // 将新节点插入到尾节点之后
        tail = newNode; // 更新尾节点
        length++; // 队列长度加1
    }

    // 出队操作
    bool deQueue(int* item) {  //保留head这个头节点
        if (isEmpty()) {
            return false; // 队列为空,无法出队
        }
        Node* temp = head->next; // 保存头节点的下一个节点
        *item = head->next->data; // 取出队首元素
        head->next = temp->next; // 移除头节点的下一个节点
        delete temp; // 释放原头节点的下一个节点
        tail = head; // 如果队列中只有一个元素,更新尾节点
        length--; // 队列长度减1
        return true;
    }

    // 获取队头元素
    bool getFront(int* item) {
        if (isEmpty()) {
            return false; // 队列为空,无法获取队首元素
        }
        *item = head->next->data;
        return true;
    }

    // 检查队列是否为空
    bool isEmpty() {
        return length == 0;
    }

    // 清空队列
    void clearQueue() {
        while (!isEmpty()) {
            deQueue(nullptr);
        }
    }

    // 显示队列内容
    void displayQueue() {
        if (isEmpty()) {
            cout << "Queue is empty." << endl;
            return;
        }
        Node* current = head->next;
        while (current != nullptr) {
            cout << current->data << " -> ";
            current = current->next;
        }
        cout << "NULL" << endl;
    }

    // 获取队列元素个数
    int queueLength() {
        return length;
    }
};

int main() {
    LinkQueue q;

    // 入队操作
    q.enQueue(1);
    q.enQueue(2);
    q.enQueue(3);

    // 显示队列内容
    q.displayQueue();

    // 获取队首元素
    int frontItem;
    if (q.getFront(&frontItem)) {
        cout << "Front item: " << frontItem << endl;
    }

    // 出队操作
    int dequeuedItem;
    while (q.deQueue(&dequeuedItem)) {
        cout << "Dequeued item: " << dequeuedItem << endl;
    }

    return 0;
}

顺序队列:

#include <iostream>

using namespace std;

class CircularQueue {
private:
    int* data; // 动态分配的数组
    int front; // 队首索引
    int rear; // 队尾索引
    int count; // 队列中元素的数量
    int capacity; // 队列的最大容量

public:
    CircularQueue(int capacity) : capacity(capacity), count(0), front(0), rear(-1) {
        data = new int[capacity]; // 动态分配数组空间
    }

    ~CircularQueue() {
        delete[] data; // 释放数组空间
    }

    bool enqueue(int value) {
        if (isFull()) {
            return false; // 队列已满,无法添加新元素
        }
        rear = (rear + 1) % capacity; // 移动队尾索引,循环到数组开头
        data[rear] = value; // 添加新元素
        count++; // 增加元素数量
        return true;
    }

    bool dequeue(int& value) {
        if (isEmpty()) {
            return false; // 队列为空,无法移除元素
        }
        value = data[front]; // 取出队首元素
        front = (front + 1) % capacity; // 移动队首索引,循环到数组开头
        count--; // 减少元素数量
        return true;
    }

    bool isEmpty() const {
        return count == 0;
    }

    bool isFull() const {
        return count == capacity;
    }

    int getSize() const {
        return count;
    }
};

int main() {
    CircularQueue q(5); // 创建一个容量为5的循环队列

    // 入队操作
    for (int i = 1; i <= 5; ++i) {
        if (!q.enqueue(i)) {
            cout << "Queue is full after adding " << i << endl;
        }
    }

    // 出队操作,并打印出队元素
    int value;
    while (!q.isEmpty()) {
        if (q.dequeue(value)) {
            cout << "Dequeue: " << value << endl;
        } else {
            cout << "Queue is empty or failed to dequeue." << endl;
        }
    }

    // 再次入队操作
    for (int i = 6; i <= 10; ++i) {
        if (!q.enqueue(i)) {
            cout << "Queue is full after adding " << i << endl;
        }
    }

    // 打印队列的当前状态
    cout << "Current elements in the queue:" << endl;
    for (int i = 0; i < q.getSize(); ++i) {
        cout << "Element at index " << ((i + front) % q.capacity) << ": " << q.data[(i + front) % q.capacity] << endl;
    }

    return 0;
}

--------------------------------------------代码借鉴于kimichat---------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值