问题在代码末尾的注释中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])
展开