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

数据结构与算法-----队列篇

时间:2015-06-18 15:22:31      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:队列   双端队列   数据结构   算法   堆栈类   

队列

1.基本特征:先进先出
2.基本操作:从后端(rear)压入(push),从前端(front)弹出(pop)
3.实现要点:初始化空间、从后端指针压入,从前端指针弹出, 循环使用,判空判满

实践1

:使用C++语言实现队列类并进行数据示例演示

#include <iostream>
using namespace std;
class Queue {
public:
    // 构造函数中分配内存空间
    Queue (size_t size) :
        m_data (new int[size]), m_size (size),
        m_rear (0), m_front (0), m_count (0) {}
    // 析构函数中释放内存空间
    ~Queue (void) {
        if (m_data) {
            delete[] m_data;
            m_data = NULL;
        }
    }
    // 压入
    void push (int data) {
        if (full ())
            throw OverFlow ();
        if (m_rear >= m_size)
            m_rear = 0;
        m_count++;
        m_data[m_rear++] = data;
    }
    // 弹出
    int pop (void) {
        if (empty ())
            throw UnderFlow ();
        if (m_front >= m_size)
            m_front = 0;
        m_count--;
        return m_data[m_front++];
    }
    // 判空
    bool empty (void) {
        return ! m_count;
    }
    // 判满
    bool full (void) {
        return m_count >= m_size;
    }
private:
    // 上溢异常
    class OverFlow : public exception {
    public:
        const char* what (void) const throw () {
            return "队列上溢!";
        }
    };
    // 下溢异常
    class UnderFlow : public exception {
    public:
        const char* what (void) const throw () {
            return "队列下溢!";
        }
    };
    int*   m_data;  // 数组
    size_t m_size;  // 容量
    size_t m_rear;  // 后端
    size_t m_front; // 前端
    size_t m_count; // 计数
};
int main (void) {
    try {
        Queue queue (5);
        queue.push (10);
        queue.push (20);
        queue.push (30);
        queue.push (40);
        queue.push (50);
//      queue.push (60);
        cout << queue.pop () << endl; // 10
        queue.push (60);
        cout << queue.pop () << endl; // 20
        cout << queue.pop () << endl; // 30
        queue.push (70);
        queue.push (80);
        cout << queue.pop () << endl; // 40
        cout << queue.pop () << endl; // 50
        cout << queue.pop () << endl; // 60
        while (! queue.empty ())
            cout << queue.pop () << endl;
//      queue.pop ();
    }
    catch (exception& ex) {
        cout << ex.what () << endl;
        return -1;
    }
    return 0;
}

输出结果
技术分享

实践2

:借用本博客目录【数据结构与算法】分类中博文《数据结构与算法—–堆栈篇》中的Stack类实现队列类并进行数据示例演示

#include <iostream>
using namespace std;
class Stack {
public:
    // 构造函数中分配内存空间
    Stack (size_t size = 10) :
        m_data (new int[size]), m_size (size),
        m_top (0) {}
    // 析构函数中释放内存空间
    ~Stack (void) {
        if (m_data) {
            delete[] m_data;
            m_data = NULL;
        }
    }
    // 压入
    void push (int data) {
        if (full ())
            throw OverFlow ();
        m_data[m_top++] = data;
    }
    // 弹出
    int pop (void) {
        if (empty ())
            throw UnderFlow ();
        return m_data[--m_top];
    }
    // 判空
    bool empty (void) {
        return ! m_top;
    }
    // 判满
    bool full (void) {
        return m_top >= m_size;
    }
private:
    // 上溢异常
    class OverFlow : public exception {
    public:
        const char* what (void) const throw () {
            return "堆栈上溢!";
        }
    };
    // 下溢异常
    class UnderFlow : public exception {
    public:
        const char* what (void) const throw () {
            return "堆栈下溢!";
        }
    };
    int*   m_data; // 数组
    size_t m_size; // 容量
    size_t m_top;  // 栈顶
};
class Queue {
public:
    Queue (size_t size) :
        m_stack1 (size), m_stack2 (size) {}
    void push (int data) {
        m_stack1.push (data);
    }
    int pop (void) {
        if (m_stack2.empty ())
            while (! m_stack1.empty ())
                m_stack2.push (m_stack1.pop ());
        return m_stack2.pop ();
    }
    bool empty (void) {
        return m_stack1.empty() && m_stack2.empty();
    }
    bool full (void) {
        return m_stack1.full ();
    }
private:
    Stack m_stack1;
    Stack m_stack2;
};
int main (void) {
    try {
        Queue queue (10);
        for (int i = 0; ! queue.full (); i++)
            queue.push (i);
//      queue.push (10);
        while (! queue.empty ())
            cout << queue.pop () << endl;
//      queue.pop ();
    }
    catch (exception& ex) {
        cout << ex.what () << endl;
        return -1;
    }
    return 0;
}

输出结果:
技术分享

数据结构与算法-----队列篇

标签:队列   双端队列   数据结构   算法   堆栈类   

原文地址:http://blog.csdn.net/hnlyyk/article/details/46547845

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