队列详细(网上罗列)

来源:互联网 发布:爱宝软件 编辑:程序博客网 时间:2024/05/21 21:46
#include <queue>#include <assert.h>/*调用的时候要有头文件: #include<stdlib.h> 或 #include<cstdlib> +#include<queue>       #include<queue>详细用法:定义一个queue的变量     queue<Type> M查看是否为空范例        M.empty()    是的话返回1,不是返回0;从已有元素后面增加元素   M.push()输出现有元素的个数      M.size()显示第一个元素          M.front()显示最后一个元素        M.back()清除第一个元素          M.pop()*/using namespace std;int _tmain(int argc, _TCHAR* argv[]){queue <int> myQ;cout<< "现在 queue 是否 empty? "<< myQ.empty() << endl; for(int i =0; i<10 ; i++){myQ.push(i);}for(int i=0; i<myQ.size(); i++){printf("myQ.size():%d\n",myQ.size());cout << myQ.front()<<endl;myQ.pop();}system("PAUSE"); return 0;}输出结果:现在 queue 是否 empty? 1myQ.size():100myQ.size():91myQ.size():82myQ.size():73myQ.size():64请按任意键继续. . .

queue的泛型编程

// queue, 队列链表,在单向列表后面加个尾指针,方便做 FIFO 的操作。由于是FIFO,对应定义了<<, >> 流直观操作template <typename T>class Queue{    struct Node    {        T dat;        struct Node * next;    };private:    Node * m_head, * m_tail;    unsigned int m_length; // 队列长度    void _clear();    void _add(const Queue<T> & a);public:    Queue();    Queue(const Queue<T> & a);    ~Queue();    // operator     Queue<T> & operator = (const Queue<T> & a){ _clear();   _add(a);    return *this;   }    Queue<T> & operator << (const T & dat){ Append(dat); return *this;} // 在队尾插入数据    Queue<T> & operator >> (T & dat);    void Append(const T & dat); // 插入尾部    void Delete();  // 删除表头数据    T & Head(); // 表头数据    T & Tail(); // 表尾数据    unsigned int Size() const{ return m_length;}    unsigned int Length() const{return m_length;}    class iterator // 定义迭代器    {    public:        iterator():m_point(NULL),m_last(NULL){  }        iterator(Queue::Node * p):m_point(p),m_last(NULL) { }        iterator(const iterator & it):m_point(it.m_point),m_last(it.m_last){    }        ~iterator(){  }        iterator & operator = (const iterator & it){m_point = it.m_point; m_last=it.m_last; return *this;   }        iterator & operator ++ (){m_last = m_point; m_point = (m_point ? m_point->next : NULL);      return *this;   }        iterator & operator ++ (int i){m_last = m_point; m_point = (m_point ? m_point->next : NULL); return *this;   }        bool operator == (const iterator &it)const { return m_point==it.m_point;}        bool operator != (const iterator &it)const { return m_point!=it.m_point;}        bool operator !(){return m_point==NULL;}        const T &operator *()const {return m_point->dat;} // 注意避免对原对象的修改,以免造成安全问题        Queue::Node * & operator ->() {return m_point;}        Queue::Node * Last() const {return m_last;}  // 返回上次操作节点,方便或移动非head节点        Queue::Node * & Node() {return m_point;}    private:        Queue::Node *m_point; // 当前节点        Queue::Node *m_last; // 上一节点,方便删除或移动非head节点的操作    };    iterator begin()const{iterator it(m_head);  return it;  }    iterator end()const{iterator it(NULL);  return it;  }};// 队列函数template <typename T>void Queue<T>::_clear(){    Node * tmp;    while(m_head){        tmp = m_head;        m_head = m_head->next;        delete tmp;    }    m_tail = NULL;    m_length = 0;}template <typename T>void Queue<T>::_add(const Queue<T> & a){    Node * tmp;    Node * ap = a.m_head;    if(ap){ // 第一个节点,主要是处理头指针        tmp = new Node;        tmp->dat = ap->dat;        if(m_tail) m_tail->next = tmp;        if(!m_head) m_head = tmp;        ap = ap->next;        m_tail = tmp;        m_length ++;    }    while(ap)    {        tmp = new Node;        tmp->dat = ap->dat;        m_tail->next = tmp;        m_tail = tmp;        ap = ap->next;        m_length ++;    }    m_tail->next = NULL;}template <typename T>Queue<T>::Queue():m_head(NULL),m_tail(NULL),m_length(0){}template <typename T>Queue<T>::Queue(const Queue<T> & a):m_head(NULL),m_tail(NULL),m_length(0){    _add(a);}template <typename T>Queue<T>::~Queue(){    _clear();}template <typename T>Queue<T> & Queue<T>::operator >> (T & dat){    if(m_length)    {        dat = m_head->dat;        Delete();    }    return *this;}template <typename T>void Queue<T>::Append(const T & dat) // 插入尾部{    Node * tmp = new Node;    tmp->dat = dat;    tmp->next = NULL;    if(m_tail) m_tail->next = tmp;    if(!m_head) m_head = tmp;    m_tail = tmp;    m_length ++;}template <typename T>void Queue<T>::Delete()  // 删除表头数据{    if(m_head){        Node * tmp = m_head;        m_head = m_head->next;        delete tmp;        m_length --;    }}template <typename T>T & Queue<T>::Head()  // 表头数据{    if(m_head) return m_head->dat;    else throw "no data in queue.";}template <typename T>T & Queue<T>::Tail() // 表尾数据{    if(m_tail) return m_tail->dat;    else throw "no data in queue.";}

测试

queue类, 结构比较简单,与单向链表结构类似。在单向列表后面加个尾指针,方便做 FIFO 的高效操作。由于是FIFO,对应定义了<<, >> 流直观操作STL的queue类元素操作使用pushpop操作,感觉可读性不够强,自己编写了一个简单的,权当学习数据结构的练习使用:  printf("\n---- 测试队列 -------------\n");  Queue<int> q,q2;  Queue<int>::iterator qit;  q << 1 << 2 << 3 << 4 << 5; // 添加数据到队列。  q2 = q;  printf("\nCheck out q all dat = "); // 输出队列数据  while(q.Length())  {  int n;    q >> n;    printf("%d ",n);  }  printf("\nCheck out q2 all dat = ");  while(q2.Length())  {  int n;    q2 >> n;    printf("%d ",n);  }
0 0
原创粉丝点击