Trying to print all nodes as a string in a linked list, however it prints location instead


Book Object with attributes: Title, Author, Year

class Book():
    def __init__(self, title = "", author = "", year = None):
        self.title = title = author
    self.year = year

def getTitle(self):
    return self.title

def getAuthor(self):

def getYear(self):
    return self.year

def getBookDetails(self):
    string = ("Title: {}, Author: {}, Year: {}"\
                 .format(self.title,, self.year))
    return string

Linked List called BookCollection

class BookCollection()
    def __init__(self):
        self.head = None
    def insertBook(self, book)
       temp = BookCollectionNode(book)
       self.head = temp

    def getAllBooksInCollection(self):
        node = self.head

Here is the issue below. I amUsing a while loop to print each node, however its printing the location of the node

        while node:
            node =

Node class called BookCollectionNode

class BookCollectionNode():

    def __init__(self, data): = data = None

    def getData(self):

    def getNext(self):

    def setData(self, newData): = newData
    def setNext(self, newNext): = newNext

Using the functions below to print the nodes of the linked list

b0 = Book("Cujo", "King, Stephen", 1981)
b1 = Book("The Shining", "King, Stephen", 1977)
b2 = Book("Ready Player One", "Cline, Ernest", 2011)
b3 = Book("Rage", "King, Stephen", 1977)

bc = BookCollection()


>Solution :


As node is a BookCollectionNode, you need to access it’s book attribut, then call getBookDetails() method

def getAllBooksInCollection(self):
    node = self.head
    while node:
        node =

And call bc.getAllBooksInCollection() without the print. As there is no return statement, it returns None that you see printed


  1. It seems you don’t really need methods to access your properties, but If you really want them, use @property

    class Book:
        def __init__(self, title="", author="", year=None):
            self._title = title
            self._author = author
            self._year = year
        def title(self):
            return self._title
        def author(self):
            return self._author
        def year(self):
            return self._year
  2. Override method __str__ to get a builtin string when printing

    class BookCollectionNode:
        def __str__(self):
            return str(
    class Book:    
        def __str__(self):
            return f"Title: {self._title}, Author: {self._author}, Year: {self._year}"
  3. For BookCollectionNode getter/setter, the proper way is

    class BookCollectionNode:
        def __init__(self, data):
            self._data = data
            self._next = None
        def data(self):
            return self._data
        def next(self):
            return self._next
        def data(self, newData):
            self._data = newData
        def next(self, newNext):
            self._next = newNext

Finally the BookCollection class becomes

class BookCollection:
    def __init__(self):
        self.head = None
    def insert_book(self, book):
        temp = BookCollectionNode(book) = self.head
        self.head = temp
    # 'print_' and not 'get_' as you don't return anything
    def print_all_books_in_collection(self):
        node = self.head
        while node:
            node =

Leave a Reply Cancel reply