How to print or iterate over an object of a linked list of linked lists?

class Information:#This is a node class
    def __init__(self, x, y, amount):
        self.x = x
        self.y = y
        self.amount = amount
        self.next = None
    
    def __iter__(self):
        return self.amount
    
    def getValue(self):
        return self.amount

class AmountsLinkedList:#Its node is Information class
    def __init__(self):
        self.start = None
    
    def insertInfo(self, x, y, amount):
        new = Information(x, y, amount)
        if self.start is None:
            self.start = new
        else:
            tmp = self.start
            while tmp.next is not None:
                tmp = tmp.next
            tmp.next = new

class Matrix:#This is a node class
    def __init__(self,counter, m, n, name, amounts):
        self.m = m
        self.n = n
        self.name = name
        self.amounts = amounts
        self.counterr = counter
        self.next = None
        self.prev = None

class MatrixLinkedList:#Its node is Matrix class
    def __init__(self):
        self.start = None

    def insertMatrix(self,counterr, m, n, name, amounts):
        new = Matrix(counterr, m, n, name, amounts)
        if self.start is None:
            self.start = new
            return new
        else:
            tmp = self.start
            while tmp.next is not None:
                tmp = tmp.next
            tmp.next = new
            new.prev = tmp
            return new
        return None
    
    def __iter__(self):
        return self.amounts.values().__iter__()

    def getMatrixByIndex(self, counterr):
        tmp = self.start
        while tmp is not None:
            if tmp.counterr == counterr:
                for amount in AmountsLinkedList:#Here I am trying to access to the data in the object of the linked list
                    print('->. '+str(tmp.amount.getValue()))
                return 'm: {}, n: {}, name: {}, amounts: {}'.format(tmp.m, tmp.n, tmp.name, tmp.amounts)
                
            
            tmp = tmp.next
        return None
    
    def length(self):
        cur = self.start
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

if __name__ == '__main__':
    ld=AmountsLinkedList()
    
    ld.insertInfo(1, 20, 'David')
    ld.insertInfo(2, 10, 'John')
    ld.insertInfo(4, 78, 'Sam')
    ld.insertInfo(12, 12, 'Peter')
    
    ld2=AmountsLinkedList()
    ld2.insertInfo(1, 20, 'David')
    ld2.insertInfo(2, 10, 'John')
    ld2.insertInfo(4, 78, 'Sam')
    
    lm = MatrixLinkedList()
    
    #Sending the two linked lists (ld and ld2) in lm linked lists
    lm.insertMatrix(1,3, 3, 'matrix_1', ld)
    lm.insertMatrix(2,4, 3, 'matrix_2', ld2)
    #Above what I'm passing in first parameter is a kind of index to then compare it in "getMatrizByIndex" method
    print('*****Matrix class has',lm.length(),'linked lists inside*****')
    print(lm.getMatrixByIndex(2))

I am trying to do a kind of linked list of linked list, and I thought I was ok but now I wanna get a linked list by its index, but that linked list has other linked list inside but in an object way, so I would like to iterate over that object.

I was trying to use __iterate__ but it doesn´t work, maybe because I am using it in a bad way. Using that I get the next in console:

hctr@DESKTOP-0VFHRDP MINGW64 ~/Documents/list_of_lists (master)
$ C:/Users/hctr/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/hctr/Documents/list_of_lists/testing.py
*****Matrix class has 2 linked lists inside*****
Traceback (most recent call last):
  File "c:/Users/hctr/Documents/list_of_lists/testing.py", line 99, in <module>
    print(lm.getMatrizByIndex(2))
  File "c:/Users/hctr/Documents/list_of_lists/testing.py", line 63, in getMatrizByIndex
    for amount in AmountsLinkedList:#Here I am trying to access to the data in the object of the linked list
TypeError: 'type' object is not iterable

And what I want is convert that object into the values so in console would look like this:

hctr@DESKTOP-0VFHRDP MINGW64 ~/Documents/list_of_lists (master)
$ C:/Users/hctr/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/hctr/Documents/list_of_lists/testing.py
*****Matrix class has 2 linked lists inside*****
m: 4, n: 3, name: matrix_2, amounts: <__main__.AmountsLinkedList object at 0x00CF5028>
David
John
Sam

I hope someone can help me, thanks in advance.



Read more here: https://stackoverflow.com/questions/68491034/how-to-print-or-iterate-over-an-object-of-a-linked-list-of-linked-lists

Content Attribution

This content was originally published by Jozhua at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: