为了实现“java单链表实现书籍管理系统”,我们需要完成以下步骤:
- 定义Book类,包括属性:书名、作者、出版社、ISBN编号等
- 定义Node类,包括属性:存储的Book对象、指向下一个节点的引用Next等
- 定义LinkedList类,包括属性:链表长度、头节点引用head等
- 实现LinkedList类的各种操作方法,例如增加、删除、修改、查找、遍历等
下面是简单的代码示例:
Book类定义
public class Book {
private String name;
private String author;
private String press;
private String isbn;
public Book(String name, String author, String press, String isbn) {
this.name = name;
this.author = author;
this.press = press;
this.isbn = isbn;
}
// Getter and Setter methods omitted for brevity
}
Node类定义
public class Node {
private Book book;
private Node next;
public Node(Book book) {
this.book = book;
this.next = null;
}
// Getter and Setter methods omitted for brevity
}
LinkedList类定义
public class LinkedList {
private Node head;
private int length;
public LinkedList() {
this.head = null;
this.length = 0;
}
// Getter and Setter methods omitted for brevity
// Add a book to the linked list
public void addBook(Book book) {
Node newNode = new Node(book);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
length++;
}
// Remove a book from the linked list
public void removeBook(Book book) {
if (head == null) {
return;
}
if (head.getBook().equals(book)) {
head = head.getNext();
length--;
return;
}
Node current = head;
while (current.getNext() != null && !current.getNext().getBook().equals(book)) {
current = current.getNext();
}
if (current.getNext() != null) {
current.setNext(current.getNext().getNext());
length--;
}
}
// Find a book in the linked list
public Book findBook(String name) {
if (head == null) {
return null;
}
Node current = head;
while (current != null) {
if (current.getBook().getName().equals(name)) {
return current.getBook();
}
current = current.getNext();
}
return null;
}
// Print the linked list
public void printLinkedList() {
Node current = head;
while (current != null) {
System.out.println(current.getBook().getName());
current = current.getNext();
}
}
}
示例1:添加书籍,删除书籍,打印链表
// 创建一个成员为头节点为空链表的单链表对象
LinkedList bookList = new LinkedList();
// 添加3本书
Book book1 = new Book("Java从入门到精通", "张三", "清华大学出版社", "978-7-302-39108-1");
Book book2 = new Book("Java编程思想", "Bruce Eckel", "机械工业出版社", "978-7-111-52404-9");
Book book3 = new Book("Java并发编程实战", "Brian Goetz", "机械工业出版社", "978-7-111-39728-0");
bookList.addBook(book1);
bookList.addBook(book2);
bookList.addBook(book3);
// 遍历链表并打印书籍名
bookList.printLinkedList();
// 输出:
// Java从入门到精通
// Java编程思想
// Java并发编程实战
// 删除一本书,并再次打印链表
bookList.removeBook(book2);
bookList.printLinkedList();
// 输出:
// Java从入门到精通
// Java并发编程实战
示例2:查找一本书
// 创建一个成员为头节点为空链表的单链表对象
LinkedList bookList = new LinkedList();
// 添加3本书
Book book1 = new Book("Java从入门到精通", "张三", "清华大学出版社", "978-7-302-39108-1");
Book book2 = new Book("Java编程思想", "Bruce Eckel", "机械工业出版社", "978-7-111-52404-9");
Book book3 = new Book("Java并发编程实战", "Brian Goetz", "机械工业出版社", "978-7-111-39728-0");
bookList.addBook(book1);
bookList.addBook(book2);
bookList.addBook(book3);
// 查找一本书
Book book = bookList.findBook("Java编程思想");
System.out.println(book.getAuthor());
// 输出:Bruce Eckel
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java单链表实现书籍管理系统 - Python技术站