Python 链表操作
您好,我需要帮助尝试了解这三个函数。我对python非常陌生 任务:Python 链表操作,python,Python,您好,我需要帮助尝试了解这三个函数。我对python非常陌生 任务: createList(srcSeq)-根据值创建链接列表 包含在srcSeq序列结构中,并返回头 新链表的引用。这些值将一次添加一个 通过将它们前置到链接列表来计算时间。myValues=[5,12,82, 32,20]myList=createList(myValues) 大小(列表)-给定 head引用(列表),返回 链表 打印列表(列表)-给定标题参考 (列表),从前到后打印链表中的值 全部在一行上,值用逗号分隔 va
- createList(srcSeq)-根据值创建链接列表
包含在srcSeq序列结构中,并返回头
新链表的引用。这些值将一次添加一个
通过将它们前置到链接列表来计算时间。myValues=[5,12,82,
32,20]myList=createList(myValues) - 大小(列表)-给定 head引用(列表),返回 链表李>
- 打印列表(列表)-给定标题参考
(列表),从前到后打印链表中的值
全部在一行上,值用逗号分隔李> - valueAt(列表,索引)-返回位于的节点中包含的值
第一个值位于位置0的给定索引位置
第二个在位置1,依此类推。如果索引超出范围,则返回
没有李> - 追加(列表,值)-将新值追加到 链接列表。假设列表至少包含一个节点李>
- concat(listA,listB)-通过
将listA的最后一个节点链接到listB的第一个节点李> - 拆分(列表)-给定标头引用(列表),拆分
将链表对半以创建两个较小的链表。头部
从列表的后半部分创建的链接列表的引用 他回来了。假设列表至少包含一个节点。如果有 链接列表中的奇数个节点,额外的节点可以是
放在两个新列表中的任何一个
def append (theList, value):
current = self.head
while current.self.next != None:
current = self.next
current.newnode
def concat(listA, listB):
if listA.tail == None:
listA.head = listB.head
else:
listA.tail.next = listB.head
elif listB.head != None:
listA.tail = listB.tail
我的全部代码:
def createList( self ):
self.head = None
temp = ListNode( value )
self.next = newnext
temp.self.next(self.head)
self.head = temp
return self.head
def size( theList ):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.self.next
return count
def printList( theList ):
node = self.head
while node:
print self.value
node = self.next
def valueAt( theList, index ):
current = head
count = 0
while current != None:
if count == index:
return current
def append( theList, value ):
current = self.head
while current.self.next != None:
current = self.next
current.newnode
def concat( listA, listB ):
if listA.tail == None:
listA.head = listB.head
else:
listA.tail.next = listB.head
elif listB.head != None:
listA.tail = listB.tail
def split( theList ):
pass
我认为你的问题没有明确说明。但就我们所拥有的: 拆分单链接列表: 但老实说,到目前为止,你所拥有的还有很多错误 如果这些列表是Python列表,那么解决方案将非常简单: 现在有一些解释:): 该函数返回两个列表的元组,其中每个列表都是提供的列表a的一半。 我上面所用的叫做,你可以把冒号字符想象成单词until。您可以提供两个_参数开始和结束,由分号分隔 示范时间! 切片不是很棒吗?它是免费的!还有一个额外的技巧,如果你想复制一个列表,你也可以通过切片来实现
b = a[:]
轰,完成了!:)
切片还有很多,你可以有两个冒号,但这是另一个时代的故事
附言:出于好奇,我做了你的家庭作业:)
类节点:
定义初始化(自身,数据):
self.data=数据
self.next=无
定义(self,*args,**kwargs):
返回str(self.data)
def创建_列表(iterable):
下一个节点=当前节点=无
对于iterable中的项目:
当前节点=节点(项目)
当前\u节点。下一个=下一个\u节点
下一个\u节点=当前\u节点
返回当前节点
def尺寸(头部):
计数=0
而负责人:
head=head.next
计数+=1
返回计数
def打印列表(标题):
而负责人:
打印(头,尾=“”)
如果head.next:
打印(“>”,end=“”)
head=head.next
打印(刷新=真)
通过
def值_(头部、索引):
而(总目):
如果指数<1:
回流头
索引-=1
head=head.next
一无所获
def追加(头、值):
而负责人:
如果不是head.next:
head.next=节点(值)
返回
head=head.next
def concat(头A、头B):
而headA:
如果不是头A.下一步:
headA.next=headB
返回
下一个
def拆分(头):
中心=头部
索引=0
而负责人:
如果索引%2:
center=center.next
head=head.next
指数+=1
headB=中心。下一个
center.next=无
返回头B
def main():
a=创建_列表([1,2,3,4,5,6,7,8,9])
打印(“打印列表::”)
打印列表(a)
打印(“\n大小:”)
印刷品(尺寸(a))
打印(“\n值位于:”)
打印(“a[-1]:%d”%value\u位于(a,-1)。数据)
打印(“a[0]:%d”%value\u位于(a,0)。数据)
打印(“a[1]:%d”%value\u位于(a,1)。数据)
打印(“a[5]:%d”%value\u位于(a,5)。数据)
打印(“a[8]:%d”%value\u位于(a,8)。数据)
#打印(“值@9%d”%value\u在(我的头,9)。数据)
打印(“\n结束(10):”)
打印列表(a)
附加(a,10)
打印列表(a)
打印(“\n类别a,b:”)
打印列表(a)
b=创建_列表([11,12,13])
打印列表(b)
康卡特(a,b)
打印列表(a)
打印(“\n打印:”)
打印列表(a)
打印(“…到…”)
b=拆分(a)
打印列表(a)
打印(“大小a:%d”%Size(a))
打印列表(b)
打印(“尺寸b:%d”%Size(b))
如果名称=“\uuuuu main\uuuuuuuu”:
main()
您到底有什么问题?有什么具体的问题吗?那就是Python列表。OP想要一个用于链表的解决方案。你说得对,我为链表添加了一个解决方案,或者至少是链表OP试图实现的类型。但是这个问题有更多的错误。我也解决了所有其他问题:)非常感谢!!我对python很陌生,你的解释对我很有帮助。对于拆分方法,哪种方法是用于链表的,是你首先完成的方法,还是你尝试我的任务的方法?我不知道你是否用单链表完成了我的作业?我只是想理解:)我是用一个单链表做作业的。看到类节点了吗?这是你的单链表中的一个项目,你可以看到它包含的只是数据和下一个。数据保持不变。。数据和下一个点在这个单链表的下一个节点上。这样想,什么是链条,而不是一连串的链环?每个链环只知道下一个链环
def split(a):
return a[:len(a)/2], a[len(a)/2:]
a = [1,2,3,4,5]
a[:2] == [1,2]
a[2:] == [3,4,5]
a[1:3] == [2,3,4]
a[2,-2] == [3]
a[-3,-2] == [3,4]
b = a[:]
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __str__(self, *args, **kwargs):
return str(self.data)
def create_list(iterable):
next_node = current_node = None
for item in iterable:
current_node = Node(item)
current_node.next = next_node
next_node = current_node
return current_node
def size(head):
count = 0
while head:
head = head.next
count += 1
return count
def print_list(head):
while head:
print(head, end="")
if head.next:
print(" > ", end="")
head = head.next
print(flush=True)
pass
def value_at(head, index):
while (head):
if index < 1:
return head
index -= 1
head = head.next
return None
def append(head, value):
while head:
if not head.next:
head.next = Node(value)
return
head = head.next
def concat(headA, headB):
while headA:
if not headA.next:
headA.next = headB
return
headA = headA.next
def split(head):
center = head
index = 0
while head:
if index % 2:
center = center.next
head = head.next
index += 1
headB = center.next
center.next = None
return headB
def main():
a = create_list([1, 2, 3, 4, 5, 6, 7, 8, 9])
print("Print list::")
print_list(a)
print("\nSize:")
print(size(a))
print("\nValue at:")
print("a[-1]: %d" % value_at(a, -1).data)
print("a[0]: %d" % value_at(a, 0).data)
print("a[1]: %d" % value_at(a, 1).data)
print("a[5]: %d" % value_at(a, 5).data)
print("a[8]: %d" % value_at(a, 8).data)
# print("value @ 9 %d"% value_at(my_head,9).data)
print("\nAppend (10):")
print_list(a)
append(a, 10)
print_list(a)
print("\nConcat a, b:")
print_list(a)
b = create_list([11, 12, 13])
print_list(b)
concat(a, b)
print_list(a)
print("\nSplit:")
print_list(a)
print("..into..")
b = split(a)
print_list(a)
print("Size a: %d" % size(a))
print_list(b)
print("Size b: %d" % size(b))
if __name__ == "__main__":
main()