单选题 适中
#19810

对如下定义的循环单链表,横线处填写( )。 // 循环单链表的结点 struct Node { int data ; // 数据域 Node * next ;…

对如下定义的循环单链表,横线处填写( )。

// 循环单链表的结点 
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; 
}
未登录也能判分,登录后才会记录
答案与解析
正确答案:C
解析
遍历循环单链表需从头结点开始,逐个访问每个结点的数据域并移动指针至下一个结点。由于链表为循环结构,终止条件不能依赖指针为空。应以回到头结点作为结束标志。首次访问时头结点必须被处理,因此需先执行输出语句再判断是否回到起点。使用do-while结构可确保至少访问一次头结点,并在每次迭代后更新指针,直到p的下一个结点为头结点时停止。