Search code examples
python-3.xlistdata-structureslinked-listtypeerror

*i was trying to add elements in a linked list in python but i am getting TypeError: '>' not supported between instances of 'int' and 'NoneType


I was trying to add elements in between a linked list of data structures in python but I am getting this error
I get this error with this code. And I have no clue how to fix a type error. And I have been at this for what seems like hours, and every change I make seems to create more of a problem. I have read through the book, and it has offered nothing. I get some things, but it is just not working for me at all. I have scoured the forums relentlessly. Thank you in advance.
I am using python 3 in vs studio code.

File "d:\coding\python_in_hole\data structure\tempCodeRunnerFile.py", line 69, in root.insert_at(2, 31) File "d:\coding\python_in_hole\data structure\tempCodeRunnerFile.py", line 45, in insert_at if index < 0 or index > self.get_length(): TypeError: '>' not supported between instances of 'int' and 'NoneType'

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


class linked_list:
    def __init__(self):
        self.head = None

    def insert_at_begining(self, data):
        node = Node(data, self.head)
        self.head = node

    def print(self):
        itr = self.head
        llstr = ''
        while itr:
            suffix = ''
            if itr.next:
                suffix = '-->'
            llstr += str(itr.data) + suffix
            itr = itr.next
        print(llstr)

    def get_length(self):
        count = 0
        itr = self.head
        while itr:
            count = count+1
            itr = itr.next
        print(count)
    
    def insert_at_end(self, data):
        if self.head is None:
            self.head = Node(data, None)
            return
        
        itr = self.head
        while itr.next:
            itr = itr.next
        itr.next = Node(data, None)

    def insert_at(self, index, data):
        if index < 0 or index > self.get_length():
            raise Exception("Invalid Index")
        
        if index == 0:
            self.insert_at_begining(data)
            return

        itr = self.head
        count = 0
        while itr:
            if count == index-1:
                node = Node(data, itr.next)
                itr.next = node
                break
            
            itr = itr.next
            count += 1

if __name__ == '__main__':
    root = linked_list() 
    root.insert_at_begining(5)
    root.insert_at_begining(15)
    root.insert_at_begining(10)
    root.insert_at_end(34)
    root.insert_at(2, 31)
    root.insert_at_end(45)
    root.print()
    root.get_length()

Solution

  • you have missed a return statment in get_length method. so your code will look like this

    class Node:
        def __init__(self, data=None, next=None):
            self.data = data
            self.next = next
    
    
    class linked_list:
        def __init__(self):
            self.head = None
    
        def insert_at_begining(self, data):
            node = Node(data, self.head)
            self.head = node
    
        def print(self):
            itr = self.head
            llstr = ''
            while itr:
                suffix = ''
                if itr.next:
                    suffix = '-->'
                llstr += str(itr.data) + suffix
                itr = itr.next
            print(llstr)
    
        def get_length(self):
            count = 0
            itr = self.head
            while itr:
                count = count+1
                itr = itr.next
            print(count)
            return count
        
        def insert_at_end(self, data):
            if self.head is None:
                self.head = Node(data, None)
                return
            
            itr = self.head
            while itr.next:
                itr = itr.next
            itr.next = Node(data, None)
    
        def insert_at(self, index, data):
            if index < 0 or index > self.get_length():
                raise Exception("Invalid Index")
            
            if index == 0:
                self.insert_at_begining(data)
                return
    
            itr = self.head
            count = 0
            while itr:
                if count == index-1:
                    node = Node(data, itr.next)
                    itr.next = node
                    break
                
                itr = itr.next
                count += 1
    
    if __name__ == '__main__':
        root = linked_list() 
        root.insert_at_begining(5)
        root.insert_at_begining(15)
        root.insert_at_begining(10)
        root.insert_at_end(34)
        root.insert_at(2, 31)
        root.insert_at_end(45)
        root.print()
        root.get_length()
    

    and the output will be like this

    4
    10-->15-->31-->5-->34-->45
    6