对如下定义的循环单链表,横线处填写( )。
// 循环单链表的结点
struct Node {
int data; // 数据域
Node* next; // 指针域
Node(int d) : data(d), next(nullptr) {}
};
// 创建一个只有一个结点的循环单链表
Node* createList(int value) {
Node* head = new Node(value);
head->next = head;
return head;
}
// 在循环单链表尾部插入新结点
void insertTail(Node* head, int value) {
Node* p = head;
while (p->next != head) {
p = p->next;
}
Node* node = new Node(value);
node->next = head;
p->next = node;
}
// 遍历并输出循环单链表
void printList(Node* head) {
if (head == nullptr) return;
Node* p = head;
_______________________ //在此处填入代码
cout << endl;
}
- A. 1 while (p != nullptr){ 2 cout << p->data << " "; 3 p = p->next; 4 }
- B. 1 while (p->next != nullptr){ 2 cout << p->data << " "; 3 p = p->next; 4 }
- C. 1 do { 2 cout << p->data << " "; 3 p = p->next; 4 } while (p != head);
- D. 1 for(; p; p=p->next){ 2 cout << p->data << " "; 3 }
正确答案:C