数据结构基于顺序存储结构的图书信息表的创建和输出实现以下功能: 1.创建空顺序 2.输入图书信息 3.位置查找 4.价格查找 5.图书名查找 6.插入 7.删除 8.输出 0.退出
时间: 2024-10-21 09:08:33 浏览: 46
首先,我们可以设计一个简单的图书信息表,每个元素包含书名、作者、价格和位置等字段。在基于顺序存储的结构(如数组或链表)中,我们可以创建一个动态数组来模拟这个表。
1. 创建空顺序存储结构:
```python
class BookInfoTable:
def __init__(self):
self.books = []
```
2. 输入图书信息:
```python
def input_book(self, title, author, price, location):
new_book = {"title": title, "author": author, "price": price, "location": location}
self.books.append(new_book)
```
3. 位置查找:
```python
def find_by_location(self, location):
for index, book in enumerate(self.books):
if book["location"] == location:
return index
return -1 # 如果未找到则返回-1
```
4. 价格查找:
```python
def find_by_price(self, price):
for book in self.books:
if book["price"] == price:
return book
return None # 如果未找到则返回None
```
5. 图书名查找:
```python
def find_by_title(self, title):
for book in self.books:
if book["title"] == title:
return book
return None
```
6. 插入图书:
```python
def insert_book(self, book_info):
self.books.append(book_info)
```
7. 删除图书:
```python
def delete_book(self, location):
if len(self.books) > 0:
self.books.pop(location)
```
8. 输出图书信息:
```python
def display_books(self):
for i, book in enumerate(self.books):
print(f"位置 {i}: {book['title']}, 作者 {book['author']}, 价格 {book['price']}")
```
9. 退出程序:
```python
def exit_program(self):
print("已退出程序")
return False
```
阅读全文