用链表实现图书馆程序的代码,功能包括向图书馆添加书、借出书、还书、显示图书馆当前状态和退出程序
时间: 2023-02-06 15:11:37 浏览: 130
C语言期末大作业-用链表实现图书管理系统
5星 · 资源好评率100%
下面是用链表实现图书馆程序的代码示例:
```python
class Book:
def __init__(self, title, author, isbn, available=True):
self.title = title
self.author = author
self.isbn = isbn
self.available = available
class Library:
def __init__(self):
self.books = []
def add_book(self, title, author, isbn):
# 向图书馆添加书
book = Book(title, author, isbn)
self.books.append(book)
def borrow_book(self, isbn):
# 借出书
for book in self.books:
if book.isbn == isbn and book.available:
book.available = False
return True
return False
def return_book(self, isbn):
# 还书
for book in self.books:
if book.isbn == isbn and not book.available:
book.available = True
return True
return False
def display_books(self):
# 显示图书馆当前状态
for book in self.books:
print(f'Title: {book.title}, Author: {book.author}, ISBN: {book.isbn}, Available: {book.available}')
def exit(self):
# 退出程序
print('Thank you for using the library system. Bye!')
exit()
# 创建图书馆对象
library = Library()
# 向图书馆添加书
library.add_book('Book A', 'Author A', '12345')
library.add_book('Book B', 'Author B', '23456')
library.add_book('Book C', 'Author C', '34567')
# 借出书
library.borrow_book('12345')
# 显示图书馆当前状态
library.display_books()
# 还书
library.return_book('12345')
# 显示图书馆当前状态
library.display_books()
# 退出程序
library.exit()
```
这个代码示例实现了一个简单的图书馆程序,可以通过链表实现对图书的增删改查操作。其中,`Book` 类表示图书,
阅读全文