带哑头的循环列表

#! /usr/bin/env python
# _*_ coding: utf-8 _*_

class Node(object):

    def __init__(self, data, next):

        self.data = data
        self.next = next

# 哑头节点的循环链表
if __name__ == "__main__":
    # 创建带有哑头节点的空的循环链表结构
    head = Node(Node, Node)
    head.next = head

    # 为空的链表插入数据
    for i in range(1,6):
        index = i
        probe = head
        while index > 0 and  head.next != None:
            probe = probe.next
            index -= 1
        probe.next = Node(i, probe.next)

    """
    可以循环打印
    <class '__main__.Node'>
    5
    4
    3
    2
    1
    <class '__main__.Node'>
    5
    4   
    3
    """
    probe = head
    cnt = 10
    while cnt > 0:
        print(probe.data)
        probe = probe.next
        cnt -= 1

猜你喜欢

转载自blog.csdn.net/liudaoqiang_tj/article/details/82111120