简体   繁体   English

无法理解Python中的链接列表

[英]Trouble Understanding Linked Lists in Python

I understand how to create a linked list without a loop, but I'm struggling to turn a regular list into a linked list. 我了解如何创建不带循环的链表,但是我正努力将常规列表转换为链表。

def addtoHead(theList, value):
    #create a new node
    node={}
    node['data'] = value
    node['next'] = theList #connect the new node to the current head
    return node #returning the new node as the head of the list
myList=None
myList=addtoHead(myList,7)
myList=addtoHead(myList,9)
myList=addtoHead(myList,10)



def printList(theList):
        ptr=theList
        while ptr!=None:
            print(ptr['data'])
            ptr=ptr['next']

Now, I want to do the same thing, except in a loop, but I'm struggling to get the logic. 现在,除了循环之外,我想做同样的事情,但是我在努力获取逻辑。

def createList(pythonList):
    node={}
    head=None
    for i in range(len(pythonList)):
        node['data'] = pythonList[i]
        node['next'] = head
        head=pythonList[i]
    printList(node)

pythonList=[7,12,14]
createList(pythonList)

My logic is that I set the data to be an element of the list, and then I connect that data type to the head. 我的逻辑是将数据设置为列表的元素,然后将数据类型连接到表头。 I then reset the head to be the data, and then continue. 然后,我将head重置为数据,然后继续。 Unfortunately, this prints 14, and then I get the error: 不幸的是,这打印出14,然后出现错误:

TypeError: 'int' object is not subscriptable

This error is for print(ptr['data']). 此错误适用于print(ptr ['data'])。 Where have I gone wrong? 我哪里出问题了?

A minor change is needed. 需要进行较小的更改。 You'll need to declare a new node inside your loop. 您需要循环中声明一个新节点。

def createList(pythonList):
    head = None
    for i in pythonList:
        new_node = {}
        new_node['data'] = i
        new_node['next'] = head
        head = new_node

At the end of the loop, you assign head to the new node created. 在循环的最后,您将head分配给创建的新节点。 In the next iteration, the next new node will reference head. 在下一次迭代中,下一个新节点将引用head。

Currently, what you're doing is not changing the reference that head points to correctly (you point it to an integer after the first iteration), generating that error. 当前,您正在做的是未正确更改head指向的引用(在第一次迭代后将其指向整数),从而生成该错误。

Outside the loop, run: 在循环之外,运行:

printList(head)    
print(head)

It'll look something like this: 它看起来像这样:

14
12
7
{'data': 14, 'next': {'data': 12, 'next': {'data': 7, 'next': None}}}

Which is exactly what your sans loop code does. 您的sans循环代码正是这样做的。

So you have 2 corrections to be made in your code. 因此,您需要在代码中进行2个更正。
1. You need to make new node for each loop 1.您需要为每个循环创建一个新节点
2. head should be new node you make. 2. head应该是您创建的新节点。

def printList(theList):
    ptr=theList
    while ptr!=None:
        print(ptr['data'])
        ptr=ptr['next']

def createList(pythonList):
    head=None
    for i in range(len(pythonList)):
        node={}
        node['data'] = pythonList[i]
        node['next'] = head
        head = node
    printList(node)

pythonList=[7,12,14]
createList(pythonList)  

Result will be 结果将是

14  
12  
7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM