#include <iostream>
using namespace std;
class Queue
{
private:
Queue(Queue &q):maxlength(0){} // 将这2者私有,从而禁用复构和=号
Queue& operator=(Queue &q){}
struct ListNode
{
int val;
struct ListNode* next;
ListNode(int v):val(v),next(0){}
~ListNode()
{
// next指针指向的内存不能再这里进行删除,需要在下个节点的析构函数中删除
cout<<"node value "<<val<<" has been deleted!"<<endl;
}
};
ListNode *head;
ListNode *tail;
const int maxlength; // const常量只能使用列表初始化,(还有引用也是)
int length;
public:
Queue():head(0), tail(0), length(0), maxlength(INT_MAX){}
Queue(int sz):head(0), tail(0), length(0), maxlength(sz){}
bool isempty()
{
return !length;
}
bool isfull()
{
return length==maxlength;
}
// 队尾加入节点
void push(int v)
{
if(length==maxlength)
{
cerr<<"!reach max length "<<maxlength<<endl;
return;
}
ListNode *tmp =
c++ 队列Queue的实现 - 练习题
最新推荐文章于 2024-05-11 21:50:14 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)