尝试将所有节点打印为链表中的字符串,但它打印的是位置

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

具有以下属性的图书对象:书名、作者、年份

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

def getTitle(self):
    return self.title

def getAuthor(self):
    return self.author

def getYear(self):
    return self.year

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

名为 BookCollection 的链表

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

    def getAllBooksInCollection(self):
        node = self.head

这是下面的问题。我正在使用 while 循环来打印每个节点,但是它打印的是节点的位置

        while node:
            print(node)
            node = node.next

节点 class 称为 BookCollectionNode

class BookCollectionNode():

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

    def getData(self):
       return self.data

    def getNext(self):
        return self.next

    def setData(self, newData):
        self.data = newData
    
    def setNext(self, newNext):
        self.next = newNext

使用下面的函数打印链表的节点

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()
bc.insertBook(b0)
bc.insertBook(b2)
bc.insertBook(b3)

print(bc.getAllBooksInCollection())

我认为,您需要向构造函数添加数据参数才能获得如下内容:

class Node:

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

# Using an object of a Book with attributes: Title, Author, Year
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)

head = Node(b0, Node(b1, Node(b2, Node(b3))))


def print_list(head, end='\n'):
    while head:
        # You can extend the below logic
        print(head.data, end=' -> ' if head.next else '') 
        head = head.next
    print(end=end)


print_list(head)

修复

由于节点是 BookCollectionNode,您需要访问它的 book 属性,然后调用 getBookDetails() 方法

def getAllBooksInCollection(self):
    node = self.head
    while node:
        print(node.data.getBookDetails())
        node = node.next

并在没有打印的情况下调用 bc.getAllBooksInCollection()。由于没有 return 语句,因此 returns None 你看到打印的


改进

  1. 看来你并不真的需要方法来访问你的属性,但如果你真的想要它们,请使用 @property

    class Book:
        def __init__(self, title="", author="", year=None):
            self._title = title
            self._author = author
            self._year = year
        @property
        def title(self):
            return self._title
        @property
        def author(self):
            return self._author
        @property
        def year(self):
            return self._year
    
  2. 覆盖方法__str__以在打印时获取内置字符串

    class BookCollectionNode:
        def __str__(self):
            return str(self.data)
    
    class Book:    
        def __str__(self):
            return f"Title: {self._title}, Author: {self._author}, Year: {self._year}"
    
  3. 对于BookCollectionNodegetter/setter,正确的做法是

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

最后 BookCollection class 变成了

class BookCollection:
    def __init__(self):
        self.head = None
    def insert_book(self, book):
        temp = BookCollectionNode(book)
        temp.next = 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:
            print(node)
            node = node.next