python中getitem_python链表__getitem__()问题

问题在代码末尾的注释中classNode():def__init__(self,initdata=None):self.data=initdataself.next=Noneself.prev=NonedefgetData(self):returnself.datadefgetNext(self):returnsel...

问题在代码末尾的注释中

class Node():

def __init__(self, initdata=None):

self.data = initdata

self.next = None

self.prev = None

def getData(self):

return self.data

def getNext(self):

return self.next

def getPrev(self):

return self.prev

def setData(self, newdata):

self.data = newdata

def setNext(self, newnext):

self.next = newnext

def setPrev(self, newprev):

self.prev = newprev

class UnorderedList():

def __init__(self, argv=None):

self.head = None

self.length = 0

def isEmpty(self):

return self.head == None

def append(self, item):

temp = Node(item)

if self.isEmpty():

self.head = temp

else:

current = self.head

while current.getNext() != None:

current = current.getNext()

current.setNext(temp)

self.length += 1

def __len__(self):

return self.length

def __str__(self):

return ""

__repr__ = __str__

def __getitem__(self, argv):

if isinstance(argv, int):

j = 0

current = self.head

while current.getNext() and j < argv:

current = current.getNext()

j += 1

return current.getData()

elif isinstance(argv, slice):

start = argv.start

stop = argv.stop

step = argv.step

current =self.head

L = []

for i in range(stop):

if i >= start and (i - start)%step == 0:

L.append(current.getData())

current = current.getNext()

return L

# 检验

print("======== 1 UnorderedList ========")

mylist = UnorderedList()

for i in range(0, 20, 2):

mylist.append(i)

print(mylist[3])

print(mylist)#为什么打印结果是空的?

print(mylist[5])

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值