帮我看看这个队列类程序

来源:百度知道 编辑:UC知道 时间:2024/05/18 00:07:25
struct NODE
{
int element;
NODE *behind;
};

class queue
{
protected:
int count;
NODE *top,*bottom;
public:
queue()
{top=bottom=NULL;count=0;}
queue(const queue &);
int EnQueue(int);
int DeQueue();
int IsEmpty();
int Clear();
int GetLength();
};

queue::queue(const queue &a)
{
top=bottom=NULL;
count=0;
if(a.count!=0)
{
NODE *temp=a.top;
while(temp)
{

(*this).EnQueue(temp->element);
temp=temp->behind;
}
}
}

int queue::EnQueue(int a)
{
if(top==NULL)
{
top=bottom=new(NODE);
if(!top)
return -1;
top->behind=NULL;
top->

else
{
bottom->behind=new(NODE); // 队尾最后一个的后面新建一个NODE
if(!bottom->behind) // 如果创建失败返回-1
return -1;
bottom=bottom->behind; // bottom指向自己的后面一个,也就是bottom后移
bottom->element=a; // 给他赋值
bottom->behind=NULL; // 后面节点赋空,其实不赋也可以,当然赋空最好
count++; // 计数器自加
}

如果是这种队列哪跟我跟你说的不一样,这种的意思是说Top指向头节点,也就是说top相当于头节点,bottom相当于尾节点。所以每次在尾节点后面添加一个node,然后再把新添加的这个当作尾节点。