欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

带哑头的循环列表

程序员文章站 2024-03-21 19:55:22
...
#! /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

 

相关标签: 循环链表