图书管理算法顺序表代码
时间: 2024-09-28 21:02:22 浏览: 30
图书管理算法中的顺序表通常是指线性数据结构,它通过数组的形式存储书籍信息。以下是使用Python语言的一个简单顺序表(ArrayList)实现,用于存储书籍(假设包含书名、作者和ID等字段):
```python
class Book:
def __init__(self, id, title, author):
self.id = id
self.title = title
self.author = author
class LibraryList:
def __init__(self):
self.books = []
# 插入书籍
def add_book(self, book):
self.books.append(book)
# 删除指定ID的书籍
def remove_book(self, id):
for index, book in enumerate(self.books):
if book.id == id:
del self.books[index]
break
# 查找书籍
def find_book(self, id):
for book in self.books:
if book.id == id:
return book
return None
# 遍历所有书籍
def display_books(self):
for book in self.books:
print(f"ID: {book.id}, Title: {book.title}, Author: {book.author}")
# 使用示例
library = LibraryList()
library.add_book(Book(1, "红楼梦", "曹雪芹"))
library.add_book(Book(2, "西游记", "吴承恩"))
library.display_books() # 显示当前所有书籍
library.remove_book(1)
library.display_books()
阅读全文