简体   繁体   中英

Implementing single linked list

I've come across a number of tutorials, which define a node class without much more. I want to create a class (much like python's native list data type) which can receive a variable number of parameters and recursively generate node instances as supporting class attributes therein.

From tutorials:

# Singly linked list
class Node():

    def __init__(self,value):

        self.value = value
        self.nextnode = None

a = Node(1)
b = Node(2)
c = Node(3)
a.nextnode = b
b.nextnode = c

I'm not sure if inheritance would be necessary; I think I would need to recursively create node objects for every element in values below. But I'm sure how to implement this.

class LinkedList():

    def __init__(self,*values):
        # insert code

Is what I'm asking above and beyond what is expected when "linked lists" come up in job interview questions? It's possible that I'm simply confused.

Edit: I came across this link How is Python's List Implemented? and read that python's native lists are dynamic arrays, and perhaps are not as similar to what I'd like to than I had previously thought.

See this article . Skip down to the section on "Circular, doubly linked list with a sentinel." It covers how to automatically generate nodes without explicitly calling them as in a,b,c = Node(1), Node(2), Node(3)

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