✏️ 纠错
第 178 题 / 共 251 题
第2题 下面C++代码实现双向链表。函数is_empty()判断链表是否为空,如链表为空返回true,否则返回false。横线处不能填写( )。
// 节点结构体
struct Node {
int data;
Node* prev;
Node* next;
};
// 双向链表结构体
struct DoubleLink {
Node* head;
Node* tail;
int size;
DoubleLink() {
head = nullptr;
tail = nullptr;
size = 0;
}
~DoubleLink() {
Node* curr = head;
while (curr) {
Node* next = curr->next;
delete curr;
curr = next;
}
}
// 判断链表是否为空
bool is_empty() const {
____________________________
}
};你真棒!
📝 题目解析
答案:C
知识点:双向链表的基本操作,空链表的判断条件
解析:链表为空时,head和tail均为nullptr,size为0,因此A、B、D均正确。C选项错误,因为head是指针,空链表中head为nullptr,访问head.data会导致空指针异常。