c++ 实现栈、单向队列、双向队列

一、栈实现

code

//
// Created by shaoxinHe on 2024/6/8.
//

#ifndef CPRIMER_MYSTACK_H
#define CPRIMER_MYSTACK_H

#include "stdexcept"
#include "iostream"

using namespace std;

struct queuNode {
    int num{};
    queuNode *next = nullptr;
};

class myStack {
private:
    queuNode *stackTop;
    int stackSize = 0;

public:
    myStack() {
        stackTop = nullptr;
        stackSize = 0;
    }

    ~myStack() {
        while (stackTop) {
            delete stackTop;
            stackTop = stackTop->next;
        }
    }

    bool push(int num) {
        if (stackSize == 0) {
            stackTop = new queuNode;
            stackTop->num = num;
            stackSize++;
            return true;
        } else if (stackSize) {
            auto *tempNode = new queuNode;
            tempNode->num = num;
            tempNode->next = stackTop;
            stackTop = tempNode;
            stackSize++;
            return true;
        }
        return false;
    }

    int pop() {
        if (!isEmpty()) {
            throw out_of_range("栈为空");
        }
        int num = stackTop->num;
        queuNode *temp = stackTop;
        stackTop = stackTop->next;
        delete temp;
        stackSize--;
        return num;
    }
    int size(){
        return this->stackSize;
    }

    bool isEmpty(){
        return size() == 0;
    }
};

#endif //CPRIMER_MYSTACK_H

二、单向队列

code

//
// Created by shaoxinHe on 2024/6/8.
//

#ifndef CPRIMER_MYQUEUE_H
#define CPRIMER_MYQUEUE_H

#include "stdexcept"

struct queueNode {
    int num;
    queuNode *next = nullptr;
};


class myQueue {
private:
    queueNode *front, *rear;
    int queueSize;
public:
    myQueue() {
        front = nullptr;
        rear = nullptr;
        queueSize = 0;
    }

    ~myQueue() {
        while (front) {
            queuNode *temp = front;
            front = front->next;
            delete temp;
        }
    }

    int size() {
        return queueSize;
    }

    bool isEmpty() {
        return queueSize == 0;
    }

    void push(int num) {
        auto *temp = new queuNode;
        temp->num = num;

        if (isEmpty()) {
            front = rear = temp;
            queueSize++;
            return;
        }
        // 队尾插入元素
        rear->next = temp;
        rear = temp;
        queueSize++;

    }

    int pop() {
        if (isEmpty()) throw std::out_of_range("数组越界");
        queuNode *temp = front;
        front = front->next;
        int num = temp->num;
        queueSize--;
        delete temp;
        return num;
    }

};

#endif //CPRIMER_MYQUEUE_H

三、双向队列

code

//
// Created by shaoxinHe on 2024/6/8.
//

#ifndef CPRIMER_DOUBLEQUEUE_H
#define CPRIMER_DOUBLEQUEUE_H

#include "stdexcept"

struct doubleNode {
    int data;
    doubleNode *pre, *next;

    explicit doubleNode(int num) {
        data = num;
        pre = next = nullptr;
    }
};

class doubleQueue {
private:
    int dqSize;
    doubleNode *front, *rear;
public:
    doubleQueue() {
        dqSize = 0;
        front = rear = nullptr;
    }

    ~doubleQueue() {
        while (front) {
            doubleNode *temp = front;
            front = front->next;
            delete temp;
        }
    }

    int size() {
        return dqSize;
    }

    bool isEmpty() {
        return size() == 0;
    }


    void pushFront(int num) {
        push(num, true);
    }

    void pushRear(int num) {
        push(num, false);
    }

    int getFront(){
        if(isEmpty()) throw std::out_of_range("双向队列为空");
        return front->data;
    }

    int getRear(){
        if(isEmpty()) throw std::out_of_range("双向队列为空");
        return rear->data;
    }

    void push(int num, bool isFront) {

        auto *node = new doubleNode(num);
        if (isEmpty()) {
            front = rear = node;
            dqSize++;
        } else if (isFront) {      // 插入元素到双向队列头
            front->pre = node;
            node->next = front;
            front = node;
            dqSize++;
        } else {                  // 插入元素到双向队列的队尾
            rear->next = node;
            node->pre = rear;
            rear = node;
            dqSize++;
        }

    }

    int popFront() {
        if (isEmpty()) throw std::out_of_range("双向队列为空");
        int num = front->data;
        dqSize--;
        doubleNode *temp = front;
        front = front->next;
        delete temp;
        return num;
    }

    int popRear() {
        if (isEmpty()) throw std::out_of_range("双向队列为空");

        int num = rear->data;
        dqSize--;
        doubleNode *temp = rear;
        rear = rear->pre;
        delete temp;
        return num;
    }
};


#endif //CPRIMER_DOUBLEQUEUE_H

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值