编写一个用链表实现的图书管理系统
时间: 2024-10-07 09:04:31 浏览: 33
编写一个图书管理系统,我们可以利用链表数据结构来存储书籍信息。这里是一个简单的概念设计:
1. **书籍类(Book)**: 它可以包含书的基本属性,如书名(title), 作者(author), ISBN(isbn) 和一个指向下一本书的指针(link)。
```java
class Book {
String title;
String author;
String isbn;
Book next;
// 构造函数
public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.next = null;
}
}
```
2. **图书管理系统(LibraryManagement)**: 包含一个链表头节点以及基本操作,比如添加新书(addBook)、删除书籍(removeBook)、查找书籍(findBook)和遍历列表(listBooks)等。
```java
class LibraryManagement {
private Book head;
// 添加新书
public void addBook(Book book) {
if (head == null) {
head = book;
} else {
Book current = head;
while (current.next != null) {
current = current.next;
}
current.next = book;
}
}
// 删除书籍
public boolean removeBook(String isbn) {
if (head == null || head.isbn.equals(isbn)) {
head = head.next;
return true;
}
Book current = head;
while (current.next != null && !current.next.isbn.equals(isbn)) {
current = current.next;
}
if (current.next != null) {
current.next = current.next.next;
return true;
}
return false;
}
// 查找书籍
public Book findBook(String isbn) {
Book current = head;
while (current != null) {
if (current.isbn.equals(isbn)) {
return current;
}
current = current.next;
}
return null;
}
// 遍历书籍列表
public void listBooks() {
Book current = head;
while (current != null) {
System.out.println("Title: " + current.title + ", Author: " + current.author);
current = current.next;
}
}
}
阅读全文