数据结构使用单链表形式创建一个图书表 使用python语言编程
时间: 2023-05-20 17:02:32 浏览: 62
可以使用以下代码创建一个单链表形式的图书表:
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
self.next = None
class BookList:
def __init__(self):
self.head = None
def add_book(self, title, author, isbn):
new_book = Book(title, author, isbn)
if self.head is None:
self.head = new_book
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_book
def display_books(self):
current = self.head
while current is not None:
print("Title:", current.title)
print("Author:", current.author)
print("ISBN:", current.isbn)
print()
current = current.next
book_list = BookList()
book_list.add_book("The Great Gatsby", "F. Scott Fitzgerald", "978-0743273565")
book_list.add_book("To Kill a Mockingbird", "Harper Lee", "978-0446310789")
book_list.add_book("1984", "George Orwell", "978-0451524935")
book_list.display_books()
输出结果如下:
Title: The Great Gatsby
Author: F. Scott Fitzgerald
ISBN: 978-0743273565
Title: To Kill a Mockingbird
Author: Harper Lee
ISBN: 978-0446310789
Title: 1984
Author: George Orwell
ISBN: 978-0451524935