C++数据结构第36课、队列的概念及实现(上)

1、队列的定义

— 队列是一种特殊的线性表

— 队列仅能在线性表的两端进行操作

队头(Front)是取出数据元素的一端,

队尾(Rear)是插入数据元素的一端。

  • 队列的特性
    队列的特性是先进先出(First in first out)

在这里插入图片描述

  • 队列的操作
    1、创建队列(Queue())
    2、销毁队列(~Queue())
    3、清空队列(clear())
    4、进队列(add())
    5、出队列(remove())
    6、获取队头元素(front())
    7、获取队列的长度(length())

  • 队列的实现
    在这里插入图片描述

Queue.h

#ifndef QUEUE_H
#define QUEUE_H

#include "Object.h"

namespace XiebsLib
{
template <typename T>
class Queue : public Object
{
public:
    virtual void add(const T& e) = 0;
    virtual void remove() = 0;
    virtual T front()const = 0;
    virtual void clear() = 0;
    virtual int length()const = 0;

};
}
#endif // QUEUE_H

在这里插入图片描述

  • StaticQueue 设计要点
    — 首先必须是类模板
    — 使用原生数组作为队列的存储空间
    — 使用模板参数决定队列的最大容量

在这里插入图片描述

  • StaticQueue 实现要点(循环计数法)
    — 关键操作:
    1、进队列:m_space[m_rear] = e; m_rear = (m_rear + 1) % N
    2、出队列:m_front = (m_front + 1) % N
    — 队列的状态:
    1、队空:(m_length == 0) && (m_fronmt == m_rear)
    2、队满:(m_length == N) && (m_front == m_rear)

StaticQueue.h

#ifndef STATICQUEUE_H
#define STATICQUEUE_H

#include "Queue.h"
#include "Exception.h"

namespace XiebsLib
{
template <typename T, int N>
class StaticQueue : public Queue<T>
{
protected:
    T m_space[N];
    int m_front;
    int m_rear;
    int m_length;
public:
    StaticQueue()
    {
        m_front = 0;
        m_rear = 0;
        m_length = 0;
    }

    int capacity()const
    {
        return N;
    }

    void add(const T& e)
    {
        if(m_length < N)
        {
            m_space[m_rear] = e;
            m_rear++;
            m_length++;
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "No space in current Queue");
        }
    }

    void remove()
    {
        if(m_length > 0)
        {
            m_front++;
            m_length--;
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "No element in current Queue");
        }
    }

    T front()const
    {
        if(m_length > 0)
        {
            return m_space[m_front];
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "No element in current Queue");
        }
    }

    void clear()
    {
        m_front = 0;
        m_rear = 0;
        m_length = 0;
    }

    int length()const
    {
        return m_length;
    }

};
}
#endif // STATICQUEUE_H

main.cpp

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

using namespace std;
using namespace XiebsLib;
int main()
{
    StaticQueue<int, 5> queue;

    for(int i = 0; i < 5; i++)
    {
        queue.add(i);
    }

    while (queue.length() > 0)
    {
        cout << queue.front() << endl;
        queue.remove();
    }
    return 0;
}

在这里插入图片描述

小结:

— 队列是一种特殊的线性表,具有先进先出的特性
— 队列只允许在线性表的两端进行操作,一端进,一端出
— StaticQueue 使用原生的数组作为内部存储空间
— StaticQueue 的最大容量由模板参数决定
— StaticQueue 采用循环计数法提高队列的操作效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值