《Python数据结构与算法分析》第三章课后习题
⽂章⽬录
前⾔
⼀、课后习题
由于时间有限,我只做了部分较容易的习题。
1. 问题13: 要我们把节点个数储存在表头中,可以通过增删操作的时候不断修改表头数据来实现,增删包括add,remove,pop等。
#Q13
class Node:
def__init__(self, initdata):
self.data = initdata
<=None
def getData(self):
return self.data
def getNext(self):
def setData(self,newdata):
self.data=newdata
def setNext(self, newnext):
<=newnext
class UnorderedList:
def__init__(self):
self.head =None#初始化头节点,接地。
def isEmpty(self):
return self.head ==None
def add(self,item,count):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
count = count+1
return count
def count2head(self,count):
self.head.setData(count)
def length(self):
return Data()
"""
⽅法:确认元素item是否在链表中
Input:元素
Output:布尔值
"""
def search(self,item):
current = self.head
found =False
while current !=None and not found:
Data()== item:
found =True
else:
else:
current = Next()
return found
"""
⽅法:移除某个链表中的item,且不假设这个元素⼀定在链表中(⽐课本中的情况更加general) Input:item
Output:None;但是链表改变
"""json轻小说网站
def remove(self,item):
current = self.head
previous =None
found =False
while not found and current !=None:
if current ==None:
print('ErrorItem is not in List')
else:
Data()== item:
found =True
else:
previous = current
current = Next()
if previous ==None:
self.head = Next()
else:
previous.Next())
self.head.setData(Data()-1)
mylist = UnorderedList()#初始化脸链表
countt=0
countt=mylist.add(31,countt)#往列表添加元素
countt=mylist.add(77,countt)
countt=mylist.add(17,countt)
countt=mylist.add(93,countt)
countt=mylist.add(26,countt)
countt=mylist.add(54,countt)
print(countt)
print(mylist.search(93))
print(mylist.length())
2. 问题14: 在问题13中已经给出。
3. 问题16: 要我们把Unorderlist类中的元素(即列表)打印出来
#Q16
"""
类:节点-----包含了修改和访问数据的⽅法,以及指向下⼀个节点的引⽤
Input:节点初始值
Output:⼀个节点
"""
class Node:
def__init__(self, initdata):
self.data = initdata
<=None
def getData(self):
return self.data菜单栏隐藏了怎么恢复手机
def getNext(self):
def setData(self,newdata):
self.data=newdata
def setNext(self, newnext):
<=newnext
"""
类:⽆序列表(特殊的链表)
"""
class UnorderedList:
def__init__(self):
self.head =None#初始化头节点,接地。
def__str__(self):
current = self.head#在经过add()就不是None了,⽽是指向第⼀个元素的地址 ss=""
while current !=None:
s =Data())
Next()
ss=ss+s+","
return ss
洛阳又发现一例def isEmpty(self):
return self.head ==None
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def length(self):
current = self.head
count =0
while current !=None:
count +=1
current = Next()
return count
"""
⽅法:确认元素item是否在链表中
Input:元素
Output:布尔值
"""
def search(self,item):
current = self.head
found =False
while current !=None and not found:
Data()== item:
found =True
else:
current = Next()
return found
"""
tool站长工具⽅法:移除某个链表中的item,假设这个元素⼀定在链表中
Input:item
Output:None;但是链表改变
"""
"""
def remove(self,item):
current = self.head
previous =None
found =False
while not found:
Data()== item:
found =True
else:
previous = current
current = Next()
if previous ==None:
self.head = Next()
else:
previous.Next())
#主函数:构建列表(链表)
楞严咒拼音全文mylist = UnorderedList()#初始化脸链表
mylist.add(31)#往列表添加元素
mylist.add(77)
mylist.add(17)
mylist.add(93)
mylist.add(26)
mylist.add(54)
print(mylist.__str__())
4. 问题18: 实现⽆序列表的⽅法:append,index,pop,insert """
类:节点-----包含了修改和访问数据的⽅法,以及指向下⼀个节点的引⽤
Input:节点初始值
Output:⼀个节点
"""
class Node:
def__init__(self, initdata):
self.data = initdata
<=None
def getData(self):
return self.data
python数据分析基础教程答案def getNext(self):
def setData(self,newdata):
self.data=newdata
def setNext(self, newnext):
<=newnext
"""
类:⽆序列表(特殊的链表)
"""
class UnorderedList:
def__init__(self):
self.head =None#初始化头节点,接地。
def__str__(self):
current = self.head#在经过add()就不是None了,⽽是指向第⼀个元素的地址 ss=""
while current !=None:
s =Data())
s =Data())
Next()
ss=ss+s+","
return ss
def isEmpty(self):
return self.head ==None
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def length(self):
current = self.head
count =0
while current !=None:
count +=1
current = Next()
return count
"""
⽅法:确认元素item是否在链表中
Input:元素
Output:布尔值
"""
def search(self,item):
current = self.head
found =False
while current !=None and not found:
Data()== item:
found =True
else:
current = Next()
return found
"""
⽅法:移除某个链表中的item,假设这个元素⼀定在链表中
Input:item
Output:None;但是链表改变
"""
def remove(self,item):
current = self.head
previous =None
found =False
while not found:
Data()== item:
found =True
else:
previous = current
current = Next()
if previous ==None:
self.head = Next()
else:
previous.Next())
def append(self,item):
current = self.head
previous =None
while current !=None:
previous = current
current = Next()
previous.setNext(Node(item))#不再需要把item的指针指向Null,因为Node(item)已经默认把item的指针设为Null
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论