‹ 上一题 下一题 ›

g5-04449. GESP-5级-2025-12-单选-第01题 未做

1000 ms 256 MB 测试点 ? GESPGESP-52025-12单选客观题5级·链表链表

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

// 循环单链表的结点
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. ```cpp
    while (p != nullptr){
    cout << p->data << " ";
    p = p->next;
    }
- **B.** ```cpp
while (p->next != nullptr){
    cout << p->data << " ";
    p = p->next;
}
  • C. ```cpp
    do {
    cout << p->data << " ";
    p = p->next;
    } while (p != head);
- **D.** ```cpp
for(; p; p=p->next){
    cout << p->data << " ";
}
未选择
输入
输出

            
答案