写链表还是不熟练。
1、删除链表
MemBlock<T> *temp;
while (frontBlock_ != backBlock_) {
temp = frontBlock_;
frontBlock_ = frontBlock_->next;
delete temp;
}
建立临时结点指向头结点,移动头结点之后删除临时结点,然后临时结点再指向新的头结点,所以临时结点的赋值要在循环里。
2、push操作和pop操作:
push操作:(队尾)
void Queue<T>::appendBlock() {
MemBlock<T> *temp = new MemBlock<T>;
backBlock_->next = temp; //在队尾压入新结点
temp->prev = backBlock_;
temp->next = NULL;
backBlock_ = temp; //更新队尾,不要忘了
ptrb_ = 0;
size_++;
}
pop操作:(队头)
void Queue<T>::reduceBlock() {
MemBlock<T> *temp = frontBlock_;
frontBlock_ = frontBlock_->next; //更新队头
delete temp; //删除旧队头
size_--;
ptrf_ = 0;
}