Search code examples
pythonclasslinked-listinner-classes

Put node class into linked list class python


This is my first time learning python , just trying to create a simple linked list

here is the code

class node:
    def __init__(self, data = None):
        self.data = data
        self.next = None
        

class linked_list:
    
    def __init__(self):
        self.head = node()
        
    def append(self, data):
        new_node = node(data)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next = new_node
    
    def length(self):
        cur = self.head
        total = 0
        while cur.next != None:
            total += 1 
            cur = cur.next
        return total
    
    @property
    def display(self):
        elems = []
        cur_node = self.head
        while cur_node.next != None:
            cur_node = cur_node.next
            elems.append(cur_node.data)
        print(elems)
    
    def get(self, index):
        if index >=  self.length():
            print('index out of range') 
            return None
        cur_idx = 0
        cur_node= self.head
        while True:
            cur_node = cur_node.next
            if cur_idx == index: return cur_node.data
            cur_idx+= 1
    
    def erase(self, index):
        if index >=  self.length():
            print('index out of range') 
            return None
        
        cur_idx = 0
        cur_node = self.head
        while True:
            last_node = cur_node
            cur_node = cur_node.next
            if cur_idx == index:
                last_node.next = cur_node.next
                return 
            cur_idx+= 1

l1 = linked_list()
l1.append(8)
l1.append(7)
l1.append(6)
l1.append(5)


print(l1.get(0))
print(l1.get(1))
print(l1.get(2))
print(l1.get(3))

everything went well except when I tried to put node class into linked list class as an inner class like below:

class linked_list:
    class node:
        def __init__(self, data = None):
            self.data = data
            self.next = None
    
    
    def __init__(self):
        self.head = node()
        
    def append(self, data):
        new_node = node(data)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next = new_node

......
(the rest are the same as the code above)

I got this error :

Traceback (most recent call last):
  File "c:\blahblah\Basic_exercise.py", line 65, in <module>
    l1 = linked_list()
  File "c:\blahblah\Basic_exercise.py", line 11, in __init__
    self.head = node()
NameError: name 'node' is not defined

1.what logic did I miss here?

2.Is there any way that I can treat node class as an inner class without getting errors?


Solution

  • The error you are seeing is due to the way you are referencing your innner class - it is a namespace issue. To disambiguate your inner node class from other node classes, you need to reference it by first using the outer class: linked_list.node. Example:

    class linked_list:
        class node:
            def __init__(self, data = None):
                self.data = data
                self.next = None
        
        
        def __init__(self):
            self.head = linked_list.node()
            
        def append(self, data):
            new_node = linked_list.node(data)
            cur = self.head
            while cur.next != None:
                cur = cur.next
            cur.next = new_node