链表做图书馆管理系统
时间: 2023-12-23 12:04:26 浏览: 120
tushuguan.cpp.rar_C语言双向链表_数据结构 管理_数据结构 管理系统_链表图书馆
5星 · 资源好评率100%
链表是一种常用的数据结构,可以用来实现图书馆管理系统。具体实现方法如下:
1.定义图书类,包括书名、作者、出版社、出版日期、ISBN号等属性。
2.定义节点类,包括图书类对象和指向下一个节点的指针。
3.定义链表类,包括头节点和尾节点。
4.实现链表的增删改查功能,包括添加图书、删除图书、修改图书信息、查找图书等。
5.实现借书还书功能,包括借出图书、归还图书等。
下面是一个简单的Python实现示例:
```python
class Book:
def __init__(self, name, author, publisher, pub_date, isbn):
self.name = name
self.author = author
self.publisher = publisher
self.pub_date = pub_date
self.isbn = isbn
class Node:
def __init__(self, book):
self.book = book
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_book(self, book):
node = Node(book)
if not self.head:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def remove_book(self, name):
if not self.head:
return
if self.head.book.name == name:
self.head = self.head.next
return
prev = self.head
curr = self.head.next
while curr:
if curr.book.name == name:
prev.next = curr.next
if not curr.next:
self.tail = prev
return
prev = curr
curr = curr.next
def modify_book(self, name, author, publisher, pub_date, isbn):
curr = self.head
while curr:
if curr.book.name == name:
curr.book.author = author
curr.book.publisher = publisher
curr.book.pub_date = pub_date
curr.book.isbn = isbn
return
curr = curr.next
def search_book(self, name):
curr = self.head
while curr:
if curr.book.name == name:
return curr.book
curr = curr.next
return None
def lend_book(self, name):
book = self.search_book(name)
if book:
self.remove_book(name)
return book
else:
return None
def return_book(self, book):
self.add_book(book)
# 示例代码
book1 = Book("Python编程从入门到实践", "Eric Matthes", "人民邮电出版社", "2016-11-1", "9787115428028")
book2 = Book("流畅的Python", "Luciano Ramalho", "人民邮电出版社", "2018-1-1", "9787111596660")
book3 = Book("Python核心编程", "Wesley Chun", "人民邮电出版社", "2014-1-1", "9787115299697")
linked_list = LinkedList()
linked_list.add_book(book1)
linked_list.add_book(book2)
linked_list.add_book(book3)
book = linked_list.lend_book("Python编程从入门到实践")
if book:
print("借出图书:", book.name)
else:
print("没有找到该图书")
book = linked_list.lend_book("Python编程从入门到实践")
if book:
print("借出图书:", book.name)
else:
print("没有找到该图书")
linked_list.return_book(book)
book = linked_list.lend_book("Python编程从入门到实践")
if book:
print("借出图书:", book.name)
else:
print("没有找到该图书")
```
阅读全文