首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

哪位高手帮小弟我看看这个链式队列错在哪儿

2013-03-20 
谁帮我看看这个链式队列错在哪儿?//file :queue_link.h#ifndef _QUEUE_LINK_H_HUMING_INCLUDE_#define _QU

谁帮我看看这个链式队列错在哪儿?

//file :queue_link.h
#ifndef _QUEUE_LINK_H_HUMING_INCLUDE_
#define _QUEUE_LINK_H_HUMING_INCLUDE_
#include<cstdio>
template <class T>
class  node
{
public:
    T data;
    node<T>* next;
    node():next(NULL) {}; //No-arg constructor
    node(T p)//Constructor
    {
        this->data=p;//"this" means "class node"
        next=NULL;
    };
};
//node defination
template <class T>
class queue
{
public:
    queue();
    bool empty();
    void pop();
    void push(T x);
    T front();
private:
    node<T> *frount,*rear;
};
template <class T>
void queue<T>::push(T x)
{
    node<T> *q=new node<T>;
    q->data=x;
    q->next=NULL;
    rear->next=q;
    rear=q;
}
template <class T>
queue<T>::queue()
{
    node<T>* frount=new node<T>;
    frount->next=NULL;
    rear=frount;
}
template <class T>
bool queue<T>::empty()
{
    if(rear==frount)  return true;
    else return false;
}
template <class T>
void queue<T>::pop()
{
    if(!empty())
    {
        node<T>* x;
        x=frount->next;
        frount->next=x->next;
        if(x->next==NULL) rear=frount;
        delete x;
    }
}
template <class T>
T queue<T>::front()
{
    if(frount->next!=NULL)return (frount->next->data);
}
#endif


[解决办法]
//file :queue_link.h
#ifndef _QUEUE_LINK_H_HUMING_INCLUDE_
#define _QUEUE_LINK_H_HUMING_INCLUDE_
#include<cstdio>
template <class T>
class  node
{
public://why don't you declare "data" and "next" as private?
    T data;
    node<T>* next;
    node():next(NULL) {}; //No-arg constructor
    node(T p)//Constructor
    {
        this->data=p;//"this" means "class node"
        next=NULL;
    };
};
//node defination
template <class T>
class queue
{
public:
    queue();
    ~queue();

// to avoid memory leak, a destructor is needed.
    bool empty();
    void pop();
    void push(T x);
    T front();
private:
    node<T> *frount,*rear;//frount? typo? do you exactly mean it? maybe "head" and "tail" are better.
};
template <class T>
void queue<T>::push(T x)
{
    node<T> *q=new node<T>;
    q->data=x;
    q->next=NULL;
    rear->next=q;
    rear=q;
}
template <class T>
queue<T>::queue()
{
    node<T>* frount=new node<T>;
    frount->next=NULL;
    rear=frount;
}
template <class T>
bool queue<T>::empty()
{
    //if(rear==frount)  return true;
    //else return false;
    return (rear==frount);//is this better?
}
template <class T>
void queue<T>::pop()
{
    if(!empty())
    {
        node<T>* x;
        x=frount->next;
        frount->next=x->next;
        if(x->next==NULL) rear=frount;
        delete x;
    }
}
template <class T>
T queue<T>::front()
{
   // if(frount->next!=NULL)return (frount->next->data);
   // what will be returned if frount->next==NULL ? I think it's NULL.
  return empty() ? NULL : frount->next->data;
}

template <class T>
queue<T>::~queue()//delete all nodes including "frount".
{
        node<T>* x;
while(frount)
{
        x=frount;
        frount=frount->next;
        delete x;
}
}
#endif

热点排行