cpp: 使用链表实现queue 模型

queue 是一个典型的容器,FILO类型的。队尾增,队头删。

下面是简单实现:

//
// Created by cat on 2018/1/6.
//

#ifndef CPP02_QUEUE_H
#define CPP02_QUEUE_H

#include "Customer.h"

namespace que {
    typedef Customer Item;

    class Queue {

        /**
         * Node 是一个节点(链表)
         */
        struct Node {
            Item item;
            Node *next;
        };
        enum {
            MAX_SIZE_DEFAULT = 10,
            DEBUG_LIFESTYLE = true,
        };
    private:
        Node *front; // 队列中的第一个节点
        Node *rear;// 队列中的最后一个节点
        unsigned int items; // 当前队列的长度
        const unsigned int queueSize; // 队列最大长度

//        Queue(const Queue &q); // 不让用
        Queue &operator=(const Queue &q); // 不让用

    public:
        Queue(const unsigned int queueSize = MAX_SIZE_DEFAULT);


        virtual ~Queue();

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

        bool isFull() { return items == queueSize; }

        int size() const { return this->items; }

        // 加到队尾
        bool enqueue(const Item &obj);

        // 删除队头
        bool dequeue(); // 这个方法不应该有参数,参数的意义是啥?

        void release();

        friend std::ostream &operator<<(std::ostream &os, const Queue &obj);
    };
}


#endif //CPP02_QUEUE_H
//
// Created by cat on 2018/1/6.
//

#include <iostream>
#include "Queue.h"

namespace que {

    Queue::Queue(const unsigned int queueSize) : queueSize(queueSize) {
        this->front = nullptr;
        this->rear = nullptr;
        this->items = 0;
        if (DEBUG_LIFESTYLE) {
            std::cout << "---Queue(" << this->items << ")|create...\n";
        }
    }

    Queue::~Queue() {
        if (DEBUG_LIFESTYLE) {
            std::cout << "prepare#Queue(" << this->items << ")|destroy.\n";
        }
        release();
        if (DEBUG_LIFESTYLE) {
            std::cout << "---Queue(" << this->items << ")|destroy...\n";
        }
    }

    bool Queue::enqueue(const Item &obj) {
        if (isFull()) {
            return false;
        }
        Node *no = new Node;
        no->item = obj;
        no->next = nullptr;
        if (this->front == nullptr || this->items == 0) {
            // add 2 the first
            this->front = no;
            this->rear = no;
//            items++;
        } else {
            // add after current rear
            Node *temp = this->rear;
            temp->next = no;
            this->rear = no;
//            items++;
        }
        items++;
        return true;
    }

    bool Queue::dequeue() {
        if (this->isEmpty()) {
            return false;
        }
        if (size() == 1) {
            this->rear = nullptr;
            delete this->front;
            this->front = nullptr;
        } else {
            Node *tn = this->front->next;
            delete this->front;
            this->front = tn;
        }
        items--;
        return true;
    }

    std::ostream &operator<<(std::ostream &os, const Queue &obj) {
        os << "Queue(items:" << obj.items << ",max:" << obj.queueSize << ")";
        return os;
    }

    void Queue::release() {
        while (this->front) {
            dequeue();
        }
    }

}
#include <iostream>
#include "Queue.h"

void foo(que::Queue queue);

using namespace que;
using namespace std;

int main() {

    {
        Queue q1 = Queue(20);
//        Queue q2 = Queue(6);
        cout << "q1=" << q1 << endl;

        Customer c1 = Customer();
        c1.set(12);
        Customer c2 = Customer();
        c2.set(33);

        q1.enqueue(c1);
        q1.enqueue(c2);
        q1.enqueue(c2);
        q1.enqueue(c1);
        cout << "X after enqueue q1=" << q1 << endl;

        q1.dequeue();
        cout << "YY after dequeue q1=" << q1 << endl;

//        q1.release();

        cout << "zz after release q1=" << q1 << endl;
//    cout << c1 << c2;

        int *arr = new int[1000];

//        foo(q2);
//        cout << "zz after release q1=...." << q1 << endl;
    }
    return 0;
}

下面是输出效果:

---Queue(0)|create...
q1=Queue(items:0,max:20)
X after enqueue q1=Queue(items:4,max:20)
YY after dequeue q1=Queue(items:3,max:20)
zz after release q1=Queue(items:3,max:20)
prepare#Queue(3)|destroy.
---Queue(0)|destroy...

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值