简体   繁体   中英

How to trace linked lists to determine resulting list?

I'm confused as to how exactly to trace linked lists to figure out what the output would be.

For example, if a linked list was like:

Head --> 17 --> 42 --> 25 --> 32 --> 6 --> None

And this was the code that went with it:

NewNode = {} 
newNode[‘data’] = Head[‘data’] 
newNode[‘next’] = None 
ptr = Head 
count = 0 
while count < 3 and ptr != None:  
   ptr = ptr[‘next’] 
   count = count + 1 
if ptr != None: 
   newNode[‘next’] = ptr[‘next’] 
   ptr[‘next’] = newNod

and

ptr = Head 
while ptr[‘data’]  != 25: 
   ptr[‘data’] = 0 
   ptr = ptr[‘next’]

I'm confused as to how one would go about showing the resulting list. I understand that ['data'] is the value, ['next'] is a placeholder for the value after, and etc. But how do you determine the resulting list?

How would your approach change if it were doubly-linked?

Also, optional bonus Q just for my own general knowledge: why would you use a linked list? They seem pretty pointless to me at the moment, but maybe I'm just missing something.

I'm confused as to how one would go about showing the resulting list

Like this:

node = Head
while node:
    print (node['data'])
    node = node['next']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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