数据结构-队列

队列:先进先出,以尾插法链表实现,增加一个指针记录链表尾,入队时插入尾部,出队时删除链表头以实现先进先出的特性

/***********************************************
     >  Filename: Queue.cpp
     >  Author:   Pyt
     >  Create:   2017-08-19 14:46:15
************************************************/

#include <iostream>
using namespace std;

template<typename T>
class Queue{
public:
    Queue();
    ~Queue();
    Queue(const Queue &t);
    Queue& operator=(const Queue &t);

    void push(const T &t);
    void pop();
    const T& front() const;
    const T& back() const;
    bool empty() const;

    void clear();
private:
    struct Node{
        Node *next;
        T val;
        Node(T x) : val(x), next(NULL){}
    };

    Node *head;
    Node *tail;
    int cursize;
};

template<typename T>
Queue<T>::Queue()
 : cursize(0), head(NULL), tail(NULL)
{
    
}

template<typename T>
Queue<T>::~Queue()
{
    clear();
}

template<typename T>
Queue<T>::Queue(const Queue &t)
 : cursize(t.cursize)
{
    Node *obj = t.head;
    if(obj)
    {
        Node *tmp = new Node(obj->val);
        head = tail = tmp;
        obj = obj->next;
    }
    else{
        head = tail = NULL;
    }
    while(obj)
    {
        Node *tmp = new Node(obj->val);
        tail->next = tmp;
        tail = tmp;
        obj = obj->next;
    }
}

template<typename T>
Queue<T>& Queue<T>::operator=(const Queue &t)
{
    if(this != &t)
    {
        Queue tmp(t);
        swap(head, tmp.head);
        swap(tail, tmp.tail);
        cursize = tmp.cursize;
    }
    return *this;
}

template<typename T>
void Queue<T>::push(const T& t)
{
    if(head)
    {
        Node *tmp = new Node(t);
        tail->next = tmp;
        tail = tmp;
    }
    else{
        head = new Node(t);
        tail  = head;
    }
    cursize++;
}

template<typename T>
void Queue<T>::pop()
{
    if(!empty())
    {
        Node *tmp = head;
        head = head->next;
        delete tmp;
        if(head == NULL)
          tail  = NULL;
        cursize--;
    }
}

template<typename T>
const T& Queue<T>::front() const
{
    if(head)
      return head->val;
}

template<typename T>
const T& Queue<T>::back() const
{
    if(tail)
      return tail->val;
}

template<typename T>
bool Queue<T>::empty() const
{
    return cursize == 0;
}

template<typename T>
void Queue<T>::clear()
{
    while(head)
    {
        Node *tmp = head;
        head = head->next;
        delete tmp;
    }
    head = tail = NULL;
    cursize = 0;
}

int main()
{
    Queue<int> q;
    for(int i=0; i<10; i++)
    {
        q.push(i);
    }
    Queue<int> q1 = q;
    q1.clear();
    cout << q1.empty() << endl;
    cout << q.front() << " " << q.back() << endl;
    while(!q.empty())
    {
        cout << q.front();
        q.pop();
    }
    cout << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值