定义一个包含图书信息的顺序表和链表
时间: 2023-03-24 14:01:54 浏览: 190
顺序表和链表
一个包含图书信息的顺序表可以定义为:
```python
class Book:
def __init__(self, title, author, isbn, publication_date):
self.title = title
self.author = author
self.isbn = isbn
self.publication_date = publication_date
class BookList:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
self.books.remove(book)
def search_by_title(self, title):
return [book for book in self.books if book.title == title]
def search_by_author(self, author):
return [book for book in self.books if book.author == author]
def search_by_isbn(self, isbn):
return [book for book in self.books if book.isbn == isbn]
def search_by_publication_date(self, publication_date):
return [book for book in self.books if book.publication_date == publication_date]
```
一个包含图书信息的链表可以定义为:
```python
class Book:
def __init__(self, title, author, isbn, publication_date):
self.title = title
self.author = author
self.isbn = isbn
self.publication_date = publication_date
self.next_book = None
class BookList:
def __init__(self):
self.head = None
def add_book(self, book):
if not self.head:
self.head = book
else:
current_book = self.head
while current_book.next_book:
current_book = current_book.next_book
current_book.next_book = book
def remove_book(self, book):
if self.head == book:
self.head = self.head.next_book
else:
current_book = self.head
while current_book.next_book != book:
current_book = current_book.next_book
current_book.next_book = current_book.next_book.next_book
def search_by_title(self, title):
books_found = []
current_book = self.head
while current_book:
if current_book.title == title:
books_found.append(current_book)
current_book = current_book.next_book
return books_found
def search_by_author(self, author):
books_found = []
current_book = self.head
while current_book:
if current_book.author == author:
books_found.append(current_book)
current_book = current_book.next_book
return books_found
def search_by_isbn(self, isbn):
books_found = []
current_book = self.head
while current_book:
if current_book.isbn == isbn:
books_found.append(current_book)
current_book = current_book.next_book
return books_found
def search_by_publication_date(self, publication_date):
books_found = []
current_book = self.head
while current_book:
if current_book.publication_date == publication_date:
books_found.append(current_book)
current_book = current_book.next_book
return books_found
```
阅读全文