数据结构与算法-----队列-使用数组(顺序结构)实现

队列

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

输出结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值