简体   繁体   中英

How to print a kind of linked list of linked lists?

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

class ValuesLinkedList:#Its node is Information class
    def __init__(self):
        self.start = None

    def insertInfo(self, x, y, value):
        new = Information(x, y, value)
        if self.start is None:
            self.start = new
        else:
            tmp = self.start
            while tmp.next is not None:
                tmp = tmp.next
            tmp.next = new

    def showInfo(self):
        tmp = self.start
        while tmp is not None:
            print(' position x: ', tmp.x, 'position y: ', tmp.y, 'value: ', tmp.value)
            tmp = tmp.next


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

    def getValues_Matrix(self):
        self.values.showInfo()


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

    def insertMatrix(self, m, n, name, values):
        new = Matrix(m, n, name, values)
        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 showMatrix(self):
        tmp = self.start
        while tmp is not None:
            print('m: ', tmp.m, 'n: ', tmp.n, 'name: ', tmp.name)
            print('------------------------------------')
            tmp.values.showInfo()
            #With above line I'am pretending to print all values that I've inserted in "ld" linkedlist
            tmp = tmp.next

if __name__ == '__main__':
    ld=ValuesLinkedList()
    ld.insertInfo(1, 20, 'David')
    ld.insertInfo(2, 10, 'John')
    ld.insertInfo(4, 78, 'Sam')
    ld.insertInfo(12, 12, 'Peter')
    ld.insertInfo(47, 79, 'Kathy')
    ld.insertInfo(85, 36, 'Ryan')
    ld.insertInfo(26, 85, 'Dan')
    lm = MatrizLinkedList()
    lm.insertMatrix(3, 3, 'matrix_1', ld)
    #Above line I'm pretending to send "ld" with all values that I've inserted
    lm.showMatrix()
    ld.showInfo()

I am trying to do a kind of linked list of linked list, I think everything was going ok until I execute lm.showMatrix() . It only prints the following:

C:\Users\edu\Documents\python\pythonDE\Testing>py testing.py
m:  3 n:  3 name:  matrix_1
------------------------------------

And what I was expecting to see was the next:

C:\Users\edu\Documents\python\pythonDE\Testing>py testing.py
m:  3 n:  3 name:  matrix_1
------------------------------------
 position x:  1 position y:  20 value:  David
 position x:  2 position y:  10 value:  John
 position x:  4 position y:  78 value:  Sam
 position x:  12 position y:  12 value:  Peter
 position x:  47 position y:  79 value:  Kathy
 position x:  85 position y:  36 value:  Ryan
 position x:  26 position y:  85 value:  Dan

The problem is in tmp.values.showInfo() what is located in "showMatrix()", and I don´t know why it doesn´t print values.

I know I can do this easier with python list, but I have to do it with linkedlist, and I know that if I add ld.showInfo() at the final of the if __name__ == '__main__': I will get the output that I want, but this is not the point, the point is do it directly from showMatrix() .

I hope someone can help me, thanks in advance.

Your Matrix constructor ignores the values argument. Instead it uses an empty list for initialising its values attribute.

So change:

    self.values = ValuesLinkedList()

To:

    self.values = values

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