首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python链表

Python链表
EN

Stack Overflow用户
提问于 2008-11-11 07:31:21
回答 25查看 326.5K关注 0票数 186

在python中使用链表的最简单方法是什么?在方案中,链表简单地由'(1 2 3 4 5)定义。事实上,Python的列表[1, 2, 3, 4, 5]和元组(1, 2, 3, 4, 5)并不是链表,并且链表具有一些很好的属性,比如常量时间连接,并且能够引用它们的不同部分。让它们成为不可变的,它们真的很容易使用!

EN

Stack Overflow用户

发布于 2008-11-11 07:54:49

前几天我写了这篇文章

代码语言:javascript
运行
复制
#! /usr/bin/env python

class Node(object):
    def __init__(self):
        self.data = None # contains the data
        self.next = None # contains the reference to the next node


class LinkedList:
    def __init__(self):
        self.cur_node = None

    def add_node(self, data):
        new_node = Node() # create a new node
        new_node.data = data
        new_node.next = self.cur_node # link the new node to the 'previous' node.
        self.cur_node = new_node #  set the current node to the new one.

    def list_print(self):
        node = self.cur_node # cant point to ll!
        while node:
            print node.data
            node = node.next



ll = LinkedList()
ll.add_node(1)
ll.add_node(2)
ll.add_node(3)

ll.list_print()
票数 72
EN
查看全部 25 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/280243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档