码迷,mamicode.com
首页 > 编程语言 > 详细

队列的实现——c++

时间:2018-10-07 22:30:50      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:出队   技术   empty   自带   hid   closed   ecb   size   color   

一、介绍

队列(Queue),是一种线性存储结构。它有以下几个特点:
(01) 队列中数据是按照"先进先出(FIFO, First-In-First-Out)"方式进出队列的。
(02) 队列只允许在"队首"进行删除操作,而在"队尾"进行插入操作。
队列通常包括的两种操作:入队列 和 出队列

 

二、实现

C++的STL中本身就包含了list类,基本上该list类就能满足我们的需求,所以很少需要我们自己来实现。本部分介绍2种C++实现。
1. C++实现一:数组实现的队列,能存储任意类型的数据。
2. C++实现二:C++的 STL 中自带的"队列"(list)的示例。

 

1.C++实现一:数组实现的队列,能存储任意类型的数据。

实现代码:.h

技术分享图片
#ifndef ARRAY_QUEUE_HXX
#define ARRAY_QUEUE_HXX

#include <iostream>
using namespace std;

template<class T> class ArrayQueue{
    public:
        ArrayQueue();
        ~ArrayQueue();

        void add(T t);
        T front();
        T pop();
        int size();
        int is_empty();

    private:
        T *arr;
        int count;
};

// 创建“队列”,默认大小是12
template<class T>
ArrayQueue<T>::ArrayQueue() 
{
    arr = new T[12];
    if (!arr) 
    {
        cout<<"arr malloc error!"<<endl;
    }
}

// 销毁“队列”
template<class T>
ArrayQueue<T>::~ArrayQueue() 
{
    if (arr) 
    {
        delete[] arr;
        arr = NULL;
    }
}

// 将val添加到队列的末尾
template<class T>
void ArrayQueue<T>::add(T t) 
{
    arr[count++] = t;
}


// 返回“队列开头元素”
template<class T>
T ArrayQueue<T>::front() 
{
    return arr[0];
}

// 返回并删除“队列末尾的元素”
template<class T>
T ArrayQueue<T>::pop() 
{
    int i = 0;;
    T ret = arr[0];

    count--;
    while (i++<count)
        arr[i-1] = arr[i];

    return ret;
}

// 返回“队列”的大小
template<class T>
int ArrayQueue<T>::size() 
{
    return count;
}

// 返回“队列”是否为空
template<class T>
int ArrayQueue<T>::is_empty()
{
    return count==0;
}


#endif
View Code

测试代码: .cpp

技术分享图片
#include <iostream>
#include "ArrayQueue.h"
using namespace std;

/**
 * C++ : 数组实现“队列”,能存储任意数据。
 *
 * @author skywang
 * @date 2013/11/07
 */
int main() 
{
    int tmp=0;
    ArrayQueue<int> *astack = new ArrayQueue<int>();

    // 将10, 20, 30 依次推入队列中
    astack->add(10);
    astack->add(20);
    astack->add(30);

    // 将“队列开头元素”赋值给tmp,并删除“该元素”
    tmp = astack->pop();
    cout<<"tmp="<<tmp<<endl;

    // 只将“队列开头的元素”赋值给tmp,不删除该元素.
    tmp = astack->front();
    cout<<"tmp="<<tmp<<endl;

    astack->add(40);

    cout<<"is_empty()="<<astack->is_empty()<<endl;
    cout<<"size()="<<astack->size()<<endl;
    while (!astack->is_empty())
    {
        tmp = astack->pop();
        cout<<tmp<<endl;
    }

    return 0;
}
View Code

 

2. C++实现二:C++的 STL 中自带的"队列"(list)的示例

#include <iostream>
#include <queue>
using namespace std;

/**
 * C++ : STL中的队列(queue)的演示程序。
 *
 * @author skywang
 * @date 2013/11/07
 */
int main ()
{
    int tmp=0;
    queue<int> iqueue;

    // 将10, 20, 30 依次加入队列的末尾
    iqueue.push(10);
    iqueue.push(20);
    iqueue.push(30);

    // 删除队列开头的元素
    iqueue.pop();

    // 将“队列开头的元素”赋值给tmp,不删除该元素.
    tmp = iqueue.front();
    cout<<"tmp="<<tmp<<endl;

    // 将40加入到队列的末尾
    iqueue.push(40);

    cout << "empty()=" << iqueue.empty() <<endl;
    cout << "size()=" << iqueue.size() <<endl;
    while (!iqueue.empty()) 
    {
        tmp = iqueue.front();
        cout<<tmp<<endl;
        iqueue.pop();  
    }

    return 0;
}

 

 

本文来自http://www.cnblogs.com/skywang12345/p/3562279.html

队列的实现——c++

标签:出队   技术   empty   自带   hid   closed   ecb   size   color   

原文地址:https://www.cnblogs.com/msymm/p/9751758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!