0

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.

petezurich
  • 9,280
  • 9
  • 43
  • 57
j9000
  • 386
  • 1
  • 3
  • 14
  • I'm not sure it is clear what is the actual question here... If you want to create multiple nodes, simply loop on `values` and create a new node for each one – Tomerikoo Jan 24 '20 at 20:06
  • 1
    `LinkedList` is the class where methods like `append`, `insert`, `find`, etc are defined. These get defined in terms of your `Node` class. For example, you typically won't build a linked list by manually linking `a`, `b`, and `c` together; in fact, `Node` itself probably never gets instantiated outside of a `LinkedList` method. Instead, you write `l = LinkedList(); l.append(1); l.append(2); l.append(3)`, or pass a list of values to the initializer: `l = LinkedList([1,2,3])`. – chepner Jan 24 '20 at 20:11
  • @chepner, yes indeed! How to define these methods is my question, I thought that was explicit but it seems my wording was more confusing than I thought it to be. – j9000 Jan 24 '20 at 20:16

1 Answers1

0

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)

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61