如何在Python中创建链接列表? [英] How can I create a linked list in Python?

查看:69
本文介绍了如何在Python中创建链接列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这样的单元格类:


#!/ usr / bin / python


import sys


类Cell:


def __init __(自我,数据,下一个=无):

self.data = data

self.next = next


def __str __(个体经营):

返回str(self.data)


def echo(self):

print self .__ str __()

with a cell class like this:

#!/usr/bin/python

import sys

class Cell:

def __init__( self, data, next=None ):
self.data = data
self.next = next

def __str__( self ):
return str( self.data )

def echo( self ):
print self.__str__()

推荐答案

Dongsheng Ruan写道:
Dongsheng Ruan wrote:

这样的单元格类:


#!/ usr / bin / python


import sys


class Cell:


def __init __(self,data,next = None):

self.data = data

self.next = next


def __str __(self):

return str( self.data)

def echo(self):

print self .__ str __()
with a cell class like this:

#!/usr/bin/python

import sys

class Cell:

def __init__( self, data, next=None ):
self.data = data
self.next = next

def __str__( self ):
return str( self.data )

def echo( self ):
print self.__str__()



如果你真的想要一个列表(因为Python定义了一个列表 - 包含所有方法)那么你应该使用Python的列表。它们非常高效和方便:


l = [Cell(1),Cell(2),Cell(3)]


但是,如果你想要的是每个单元格引用一个下一个单元格(毕竟它是链接列表的作用),那么你已经拥有了下一个属性。 (在其他语言中,您可以将''next''作为指针实现,而在Python中我们将其称为引用 - 但它也是相同的。)以这种方式创建它:


c = Cell(3)

b = Cell(2,c)

a = Cell(1,b)





a =单元格(1,单元格(2,单元格(3)))


但是,我会重复一遍:这个概念如果是具有指针数据类型的语言的图形链接列表。 Python通过允许属性包含对其他对象的引用来抽象它。但是,如果你可以使用Python的列表数据结构而不是试图用现代语言模拟过时的概念,你会好得多。


Gary Herron


如何使用

struct-s和数组(或指针)实现链接列表,例如c中的实现。

来简化工作原理


Gary Herron je napisao / la:
How can I implement a linked list like the implementations in c with
struct-s and arrays (or pointers).
to simbolize the working principe

Gary Herron je napisao/la:

Dongsheng Ruan写道:
Dongsheng Ruan wrote:

这样的单元格类:


#!/ usr / bin / python


import sys


class Cell:


def __init __(self,data,next = None):

self.data = data

self.next = next


def __str __(self):

return str( self.data)

def echo(self):

print self .__ str __()
with a cell class like this:

#!/usr/bin/python

import sys

class Cell:

def __init__( self, data, next=None ):
self.data = data
self.next = next

def __str__( self ):
return str( self.data )

def echo( self ):
print self.__str__()



如果你真的很想列表(因为Python定义了一个列表 - 包含所有方法),那么你应该使用Python的列表。它们非常高效和方便:


l = [Cell(1),Cell(2),Cell(3)]


但是,如果你想要的是每个单元格引用一个下一个单元格(毕竟它是链接列表的作用),那么你已经拥有了下一个属性。 (在其他语言中,您可以将''next''作为指针实现,而在Python中我们将其称为引用 - 但它也是相同的。)以这种方式创建它:


c = Cell(3)

b = Cell(2,c)

a = Cell(1,b)





a =单元格(1,单元格(2,单元格(3)))


但是,我会重复一遍:这个概念如果是具有指针数据类型的语言的图形链接列表。 Python通过允许属性包含对其他对象的引用来抽象它。但是,如果您可以使用Python的列表数据结构而不是尝试用现代语言模拟过时的概念,那么你会好得多。


Gary Herron

If you really want a list (as Python defines a list - with all the methods) then you should use Python''s lists. They are quite efficient and convenient:

l = [Cell(1), Cell(2), Cell(3)]

However, if what you want is each cell to refer to a next cell (which after all is what a linked list does), then you already have it with the next attribute. (In other languages you might implement ''next'' as a pointer, while in Python we call it a reference -- but it amounts to the same thing.) Create it this way:

c = Cell(3)
b = Cell(2, c)
a = Cell(1, b)

or

a = Cell(1, Cell(2, Cell(3)))

However, I''ll repeat this: The concept of a linked list if a figment of languages with pointer data types. Python abstracts that by allowing attributes to contain references to other objects. However, you''re much better off if you can use Python''s list data structure rather than try to emulate an outdated concept in a modern language.

Gary Herron


感谢您的帮助。

我是新的CS学生,用AHO的书来学习数据结构和算法

,标题相同。


这本书是用Pascal完成的,可能是一种过时的语言。


但是,我的教师可能希望我们通过不使用Python中的内置列表来更好地理解ADT列表




" Gary Herron" < gh ***** @ islandtraining.comwrote in message

news:ma ************************* ************** @ pyt hon.org ...
Thanks for your kindly help.
I am new CS student taking datat structure and algorithms with AHO''s book
with the same title.

The book is done in Pascal, which might be an outdated language.

However, my instructor probably wants us to understand the list ADT better
by not using the built in list in Python.

"Gary Herron" <gh*****@islandtraining.comwrote in message
news:ma***************************************@pyt hon.org...

Dongsheng Ruan写道:
Dongsheng Ruan wrote:

>带有这样的单元格类:

#!/ usr / bin / python

import sys
类Cell:

def __init __(self,data,next = None):
self.data = data
self.next = next

def __str __(self):
返回str(self.data)
def echo(self):
print self .__ str __()
>with a cell class like this:

#!/usr/bin/python

import sys

class Cell:

def __init__( self, data, next=None ):
self.data = data
self.next = next

def __str__( self ):
return str( self.data )

def echo( self ):
print self.__str__()



如果你真的想要一个列表(因为Python定义了一个列表 - 使用所有的

方法),那么你应该使用Python的列表。它们非常高效且方便:


l = [Cell(1),Cell(2),Cell(3)]


然而,如果你想要的是每个单元格引用一个下一个单元格(毕竟这是链接列表的
),那么你已经将它与

下一个属性。 (在其他语言中,你可以将''next''实现为

指针,而在Python中我们称之为引用 - 但它相当于

同样的东西。以这种方式创建:


c =单元格(3)

b =单元格(2,c)a =单元格(1,b)





a =单元格(1,单元格(2,单元格(3)))


但是,我将重复一遍:链接列表的概念,如果是带有指针数据类型的
语言的图形。 Python通过允许

属性包含对其他对象的引用来抽象出来。但是,如果您可以使用Python的列表数据结构而不是尝试使用现代语言模拟过时的概念,那么您的价格会更好。<\\ n>
br />

Gary Herron

If you really want a list (as Python defines a list - with all the
methods) then you should use Python''s lists. They are quite efficient and
convenient:

l = [Cell(1), Cell(2), Cell(3)]

However, if what you want is each cell to refer to a next cell (which
after all is what a linked list does), then you already have it with the
next attribute. (In other languages you might implement ''next'' as a
pointer, while in Python we call it a reference -- but it amounts to the
same thing.) Create it this way:

c = Cell(3)
b = Cell(2, c) a = Cell(1, b)

or

a = Cell(1, Cell(2, Cell(3)))

However, I''ll repeat this: The concept of a linked list if a figment of
languages with pointer data types. Python abstracts that by allowing
attributes to contain references to other objects. However, you''re much
better off if you can use Python''s list data structure rather than try to
emulate an outdated concept in a modern language.

Gary Herron



这篇关于如何在Python中创建链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆