单选题 适中
#16974

对如下定义的循环单链表,`print_list` 函数横线处填写()。 class Node : def __init__ ( self , data ) :…

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

class Node:
def __init__ (self , data) :
    self.data = data
    self.next = None
def create_list(value) :
    head = Node(value)
    head.next = head
    return head
def insert_tail(head , value) :
    p = head
    while p.next != head:
        p = p.next
    node = Node(value)
    node.next = head
    p.next = node
def print_list(head) :
    # 填空位置
    while True:
        print(p.data , end="  ")
        p = p.next
        if p == head :
            break
    print()
未登录也能判分,登录后才会记录
答案与解析
正确答案:C
解析
循环单链表打印需要先初始化遍历指针p,若表头不为空则令p=head,题目选项缺失C、D,标准答案为C,核心逻辑是先给p赋值链表起始节点才能进入循环打印。