链队

#include <iostream>

using namespace std;
int poporder = 0;   
class node
{
    friend class Queue; //友元类
private:
    long data;  
    node * next;    //递归定义指向后继结点的指针 
};

class Queue
{
public:
    Queue();
    ~Queue();
    void destroy();         //销毁一个队列 
    bool push(long item);   //把数据压进队列
    bool pop();             //出队列
private:
    node *rear;
    node *front;
    int Queuelenth; //当前队列长度    
};
Queue::Queue()
{   
    rear = new node;
    rear->next = NULL;
    front = rear;
    Queuelenth = 0;
} 
Queue::~Queue()
{
    destroy();
    delete rear;
    delete front; 
} 
void Queue::destroy()
{
    for(int i = 0;i < Queuelenth; i++ )
    {   
        pop();
    }
}


/********入队列*******/ 
bool Queue::push(long item)
{   
    node *newnodep = new node;  
    newnodep->data = item;
    rear->next = newnodep;
    rear = newnodep;
    Queuelenth++;
    return true; 
}
/******出队列********/ 
bool Queue::pop()
{   
    if(Queuelenth == 0) 
        return false;
    node *popnodep =  front->next;
    front = front->next; 
    Queuelenth--;
    return true;
}
上一篇:C++课设


下一篇:文本统计器