Python简单模拟单向链表数据结构

class Nodes:
    def __init__(self, v):
        self._v = v
        self._next = None

    def get_v(self):
        return self._v

    def get_next(self):
        return self._next


class NodeLinked:
    def __init__(self):
        self._head = None
        self._pre = None
        self._tail = None

    def append(self, node):
        if self._head is None:
            self._head = node
            self._pre = node
        else:
            self._pre._next = node
            self._pre = node
        self._tail = node

    def show_nodes(self):
        item = self._head
        while item:
            print(item.get_v())
            item = item.get_next()


n1 = Nodes("我是n1")
n2 = Nodes("我是n2")
n3 = Nodes("我是n3")

n_link = NodeLinked()
n_link.append(n3)
n_link.append(n1)
n_link.append(n2)

n_link.show_nodes()

 

posted @ 2021-12-09 21:03  kunsile  阅读(35)  评论(0编辑  收藏  举报