动态队列
Dynamic Queue C++
template<class T>
class QueueD: public IQueue<T>{
private:
Node<T> *QFront, *QRear;
public:
QueueD(): QFront(NULL), QRear(NULL){}
bool empty()const{
return QFront==NULL;
}
bool enqueue(const T &info){
Node<T> *p=new Node<T>(info,NULL);
if(QFront==NULL)
QFront=p;
else
QRear->setNext(p);
QRear=p;
return true;
}
bool dequeue(T &info){
if (empty()) return false;
else{
info=QFront->getInfo();
Node<T> *p=QFront;
if(QRear==QFront)
QRear=NULL;
QFront=QFront->getNext();
delete p;
return true;
}
}
};
template<class T>
class Node{
private:
T info;
Node *next;
public:
Node(const T &c, Node *p): info(c), next(p){
}
Node *getNext()const{
return next;
}
void setNext(Node *p){
next=p;
}
T &getInfo(){
return info;
}
};
我一直在努力更好地理解c++,我想知道你们是否可以解释我这里不理解的几行代码。
QFront=QFront->getNext();
QFront如何知道下一个节点是哪个?在代码中,它仅为QRear设置。
if(QRear==QFront) {QRear=NULL;}
为什么这是必要的?
当队列为空QFront == QRear
时,因此当您为QRear
设置下一个元素时,您也将有效地为QFront设置下一个元素。QFront就是这样知道下一个是哪个元素的。
我认为dequeue
中设置QRear
为NULL
是不必要的,因为enqueue
在空队列的情况下不使用它。
相关文章:
- boost::进程间消息队列引发错误
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- C++中的动态铸造故障
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Android NDK传感器向事件队列报告奇怪的间隔
- 内联映射初始化的动态atexit析构函数崩溃
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- C++ 调用析构函数后动态模板队列"double free or corruption (out)"
- 在C++中建立动态队列
- 如何显示动态堆栈和队列(c++)中的所有元素
- 使用链表的动态队列//总线错误:10(C++)
- 我应该在高度动态的系统中使用优先级队列来调度任务(函数等)吗
- 如何在c++中动态创建一个具有列表底层数据结构的队列
- 创建动态循环队列,元素输出正常,但程序崩溃
- 动态队列
- 如何使提升::无锁::队列动态调整大小