python用链表写图书管理系统
时间: 2023-07-18 14:46:09 浏览: 152
tushuguanlixitong.rar_图书管理系统
好的,我可以给你提供一个基于链表的简单图书管理系统的实现思路,具体代码实现需要根据具体需求进行修改。
1. 定义图书节点类(BookNode),包括书名(title)、作者(author)、ISBN(isbn)、价格(price)等属性,以及指向下一本书的指针(next)。
2. 定义图书链表类(BookList),包括头节点(head)和链表长度(size)两个属性,以及添加图书(add_book)、删除图书(delete_book)、查找图书(find_book)等方法。
3. 添加图书方法(add_book):创建一个新的图书节点,将其插入到链表的尾部。
4. 删除图书方法(delete_book):遍历整个链表,找到要删除的图书节点,将其从链表中删除。
5. 查找图书方法(find_book):遍历整个链表,找到匹配书名或ISBN的图书节点,返回该节点的信息。
下面是一个简单的代码实现示例:
```python
class BookNode:
def __init__(self, title, author, isbn, price):
self.title = title
self.author = author
self.isbn = isbn
self.price = price
self.next = None
class BookList:
def __init__(self):
self.head = None
self.size = 0
def add_book(self, title, author, isbn, price):
new_book = BookNode(title, author, isbn, price)
if not self.head:
self.head = new_book
else:
current_book = self.head
while current_book.next:
current_book = current_book.next
current_book.next = new_book
self.size += 1
def delete_book(self, isbn):
current_book = self.head
previous_book = None
while current_book:
if current_book.isbn == isbn:
if previous_book:
previous_book.next = current_book.next
else:
self.head = current_book.next
self.size -= 1
return True
previous_book = current_book
current_book = current_book.next
return False
def find_book(self, keyword):
current_book = self.head
while current_book:
if keyword in current_book.title or keyword == current_book.isbn:
print(f"Title: {current_book.title}, Author: {current_book.author}, ISBN: {current_book.isbn}, Price: {current_book.price}")
current_book = current_book.next
if __name__ == '__main__':
book_list = BookList()
book_list.add_book("Python Crash Course", "Eric Matthes", "978-1-59327-603-4", 25.0)
book_list.add_book("Fluent Python", "Luciano Ramalho", "978-1-491-94600-8", 39.99)
book_list.add_book("Effective Python", "Brett Slatkin", "978-0-13-485398-7", 29.99)
book_list.delete_book("978-1-59327-603-4")
book_list.find_book("Python")
```
这个示例代码只是一个简单的实现,还有很多方面可以优化和改进,比如增加更新图书信息的方法、对图书链表进行排序等。
阅读全文