使用Python语言创建单链表的图书管理系统
时间: 2023-05-20 12:02:35 浏览: 137
可以使用Python语言创建单链表的图书管理系统。首先,需要定义一个Book类,包含书名、作者、出版社等属性。然后,定义一个LinkedList类,包含添加、删除、查找等方法。最后,可以编写一个主程序,实现图书管理系统的各种功能。以下是示例代码:
```
class Book:
def __init__(self, title, author, publisher):
self.title = title
self.author = author
self.publisher = publisher
class Node:
def __init__(self, book=None, next_node=None):
self.book = book
self.next_node = next_node
class LinkedList:
def __init__(self):
self.head = None
def add_book(self, book):
new_node = Node(book)
if not self.head:
self.head = new_node
else:
current_node = self.head
while current_node.next_node:
current_node = current_node.next_node
current_node.next_node = new_node
def remove_book(self, title):
current_node = self.head
previous_node = None
while current_node and current_node.book.title != title:
previous_node = current_node
current_node = current_node.next_node
if current_node:
if previous_node:
previous_node.next_node = current_node.next_node
else:
self.head = current_node.next_node
def find_book(self, title):
current_node = self.head
while current_node and current_node.book.title != title:
current_node = current_node.next_node
if current_node:
return current_node.book
else:
return None
if __name__ == '__main__':
linked_list = LinkedList()
linked_list.add_book(Book('Python入门教程', '张三', '人民邮电出版社'))
linked_list.add_book(Book('Java编程思想', '李四', '机械工业出版社'))
linked_list.add_book(Book('C++ Primer', '王五', '电子工业出版社'))
linked_list.remove_book('Java编程思想')
print(linked_list.find_book('Python入门教程').author)
```
这段代码实现了一个简单的图书管理系统,可以添加、删除、查找图书。
阅读全文