欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第36课 - 队列的概念及实现(上)

程序员文章站 2022-07-14 12:33:42
...

1、队列的概念及实现

            队列是—种特殊的线性表 

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

      -队头(Front) : 取出数据元素的—端 

      -队尾(Rear) : 插入数据元素的—端

 

队列的特性 

      -先进先出(First In First Out

第36课 - 队列的概念及实现(上)

队列的操作 

      -创建队列( Queue( ) )  

      -销毁队列(~Queue( ) ) 

      -清空队列( clear( ) ) 

      -进队列( add( ) ) 

      -出队列( remove( ) ) 

      -获取队头元素( front( ) ) 

      -获取队列的长度( length( ) )

队列的实现

第36课 - 队列的概念及实现(上)

第36课 - 队列的概念及实现(上)

队列的顺序实现 

第36课 - 队列的概念及实现(上)

StaticQueue设计要点  

      -使用原生数组作为队列的存储空间 

      -使用模板参数决定队列的最大容量 

 第36课 - 队列的概念及实现(上)

StaticQueue实现要点(循环计数法) 

      -关键操作 

                         进队列: m_space[m_rear] = e; m_rear = (m_rear + 1) % N; 

                         出队列: m_front = (m_front + 1) % N; 

      -队列的状态 

                         队空: (m_length == 0) && (m_front == m_rear) 

                         队满: (m_length == N) && (m_front == m_rear)

 

 

2、编程实验 

基于顺序存储结构的队列     StaticQueue.h

Queue.h

#ifndef QUEUE_H
#define QUEUE_H

#include "Object.h"

namespace DTLib
{

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.h

#ifndef STATICQUEUE_H
#define STATICQUEUE_H

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

namespace DTLib
{

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_rear + 1) % N;
            m_length++;
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException,"No space in current queue ...");
        }
    }
    void remove()
    {
        if(m_length > 0)
        {
            m_front = (m_front + 1) % N;
            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 DTLib;



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;
}

 第36课 - 队列的概念及实现(上)

 

 

3、小结

            队列是一种特殊的线性表,具有先进先出的特性 

            队列只允许在线性表的两端进行操作,—端进,—端出

            StaticQueue使用原生数组作为内部存储空间

            StaticQueue最大容量由模板参数决定 

            StaticQueue采用循环计数法提高队列操作的效率