基于链表的图书信息管理Python版
时间: 2024-10-13 15:10:59 浏览: 122
基于链表的图书信息管理可以是一个简单的Python实现,它通常会包含以下几个部分:
1. **节点类**(BookNode):每个节点代表一本书,包含书名(title)、作者(author)等基本信息,以及指向下一个节点的指针。
```python
class BookNode:
def __init__(self, title, author):
self.title = title
self.author = author
self.next = None
```
2. **链表类**(BookList):负责管理节点,并提供添加、删除和查找书籍的功能。
```python
class BookList:
def __init__(self):
self.head = None
# 添加书籍
def add_book(self, title, author):
new_node = BookNode(title, author)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
# 删除书籍
def delete_book(self, title):
if not self.head:
return
if self.head.title == title:
self.head = self.head.next
return
current = self.head
while current.next and current.next.title != title:
current = current.next
if current.next:
current.next = current.next.next
# 查找书籍
def search_book(self, title):
current = self.head
while current:
if current.title == title:
return True
current = current.next
return False
```
阅读全文