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

数据结构之——基于链表的队列的C++模板实现

时间:2017-06-14 22:30:01      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:bsp   ext   next   申请   ++   ada   空间   logs   void   

//节点的结构
template<typename T>
struct node
{
    T data;
    node<T>* next;
    node():next(nullptr){};
    node(T t):data(t),next(nullptr){};
}
//模板类构造队列类
template<typename T>
Class Myqueue
{
public:
    Myqueue():head(nullptr),tail(nullptr),count(0)
    {
        head=new node<T>();
        tail=head;
        count=0;
    }
    void enqueue(T &x);
    T front();
    void dequeue();
    int counts();
    bool empty();
    ~Myqueue()
    {
        while(head->next!=nullptr)
        {
            node<T>* p=head;
            head=head->next;
        }
    }
private:
    int count;
    node<T>* head;
    node<T>* tail;
}
template<typename T>
void Myqueue<T>::enqueue(T &x)
{
    node<T>* p= new node<T>(x);//申请空间
    head->next=p;
    head=p;
    count++;
}
template<typename T>
bool Myqueue<T>::empty()
{
    return count==0;
}
template<typename T>
T MyStack<T>::front()
{
    if(!empty())
        return head->next->dada;
}
template<typename T>
void MyStack<T>::pop()
{
    if(!empty())
    {
        node<T>* del=head->next;
        head->next=haed->next->next;
        delete del;
        count--;
    }
}
template<typename T>
int MyStack<T>::counts()
{
    return count;
}

 

数据结构之——基于链表的队列的C++模板实现

标签:bsp   ext   next   申请   ++   ada   空间   logs   void   

原文地址:http://www.cnblogs.com/gaoxiaoyan123/p/7011036.html

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