我正在制作一个程序,其中程序显示一些选项,如插入书,删除书等。该程序要求我们通过插入方法输入有关书籍的某些细节。
我已经制作了所有方法,但我不知道如何制作删除方法。这是我在课堂上的任务。首先,我在编程方面非常弱,特别是在链表中。因此,任何有关删除方法的帮助将不胜感激。这是我的代码......
import java.util.Scanner;
public class BookApplication {
static Book head, pointer;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Smart Book Store");
System.out.println("Please choose an option from the list below");
int choice = 0;
do {
System.out.println("1. Insert Book\n2. Delete Book\n3. Search Book\n4. Update Book\n5. View Book\n6. Exit");
choice = scan.nextInt();
try {
choice = Integer.parseInt(scan.nextLine());
} catch (Exception e) {
switch (choice) {
case 1:
addBook();
break;
case 2:
case 3:
searchBook();
break;
case 4:
updateBook();
viewBook();
break;
case 5:
viewBook();
break;
case 6:
scan.close();
System.exit(0);
break;
default:
System.out.println("Please choose from 1 to 5");
break;
}
}
} while (true);
}
static void addBook() {
if (head == null) {
String details[] = enterDetails();
pointer = new Book(details[0], details[1], details[2]);
head = pointer;
pointer.next = null;
} else {
String details[] = enterDetails();
pointer.next = new Book(details[0], details[1], details[2]);
pointer = pointer.next;
}
}
static String[] enterDetails() {
String[] details = new String[4];
try {
String title;
String ISBN;
String authors;
System.out.println("Please enter Book title");
title = scan.nextLine();
System.out.println("Please enter ISBN of book");
ISBN = scan.nextLine();
System.out.println("Please enter book's Author(s)");
authors = scan.nextLine();
details[0] = title;
details[1] = ISBN;
details[2] = authors;
} catch (Exception e) {
e.printStackTrace();
}
return details;
}
private static void searchBook() {
System.out.println();
System.out.println("1. Search by TITLE");
System.out.println("2. Search by ISBN");
System.out.println("3. Search by AUTHOR");
int choice = 0;
choice: try {
choice = Integer.parseInt(scan.nextLine());
} catch (Exception e) {
System.out.println();
System.out.println("PLEASE ENTER VALUE BETWEEN 1 - 3");
break choice;
}
switch (choice) {
case 1:
System.out.println("Please enter Title of BOOK");
String title = scan.nextLine();
if (head == null) {
System.out.println("List is EMPTY !");
return;
} else {
System.out.println();
System.out.println("BOOK(S) IN THE SYSTEM ARE: ");
System.out.println();
pointer = head;
while (pointer != null) {
if (pointer.getTitle().equals(title)) {
System.out.println(pointer.getBook());
}
pointer = pointer.next;
}
}
break;
case 2:
System.out.println("Please enter ISBN of BOOK");
String ISBN = scan.nextLine();
if (head == null) {
System.out.println("List is EMPTY !");
return;
} else {
System.out.println();
System.out.println("BOOK(S) IN THE SYSTEM ARE: ");
System.out.println();
pointer = head;
while (pointer != null) {
if (pointer.getISBN().equals(ISBN)) {
System.out.println(pointer.getBook());
break;
}
pointer = pointer.next;
}
}
break;
case 3:
System.out.println("Please enter Author(s) of BOOK");
String authors = scan.nextLine();
if (head == null) {
System.out.println("List is EMPTY !");
return;
} else {
System.out.println();
System.out.println("BOOK(S) IN THE SYSTEM ARE: ");
System.out.println();
pointer = head;
while (pointer != null) {
if (pointer.getAuthors().contains(authors)) {
System.out.println(pointer.getBook());
break;
}
pointer = pointer.next;
}
}
break;
default:
System.out.println("PLEASE ENTER VALUE BETWEEN 1 - 5");
}
}
static void updateBook() {
System.out.println();
System.out.println("1. Update by TITLE");
System.out.println("2. Update by ISBN");
System.out.println("3. Update by AUTHOR");
int choice = 0;
choice: try {
choice = Integer.parseInt(scan.nextLine());
} catch (Exception e) {
System.out.println();
System.out.println("PLEASE ENTER VALUE BETWEEN 1 - 3");
break choice;
}
switch (choice) {
case 1:
System.out.println("Please provide the Title of Book you want to update: ");
String another1 = scan.nextLine();
if (head == null) {
System.out.println("No Books to Update");
return;
} else {
Boolean found = false;
pointer = head;
while (!found & pointer != null) {
if (pointer.getTitle().equals(another1)) {
found = true;
System.out.println("Here is the current book:" + pointer.getBook());
System.out.println("Please enter the new Title:");
pointer.setTitle(scan.nextLine());
System.out.println("Please Enter the new ISBN:");
pointer.setISBN(scan.nextLine());
System.out.println("Please Enter the new Author:");
pointer.setAuthors(scan.nextLine());
}
pointer = pointer.next;
}
}
break;
case 2:
System.out.println("Please provide the ISBN of Book you want to update: ");
String ISBN = scan.nextLine();
if (head == null) {
System.out.println("No Books to Update!");
return;
} else {
Boolean found = false;
pointer = head;
while (!found && pointer != null) {
if (pointer.getISBN().equals(ISBN)) {
found = true;
System.out.println("Here is the current book:" + pointer.getBook());
System.out.println("Please enter the new Title:");
pointer.setTitle(scan.nextLine());
System.out.println("Please Enter the new ISBN:");
pointer.setISBN(scan.nextLine());
System.out.println("Please Enter the new Author:");
pointer.setAuthors(scan.nextLine());
}
pointer = pointer.next;
}
}
case 3:
System.out.println("Please enter Author(s) of the Book you want to Update: ");
String upauthor1 = scan.nextLine();
if (head == null) {
System.out.println("List is EMPTY !");
return;
} else {
Boolean found = false;
pointer = head;
while (!found && pointer != null) {
if (pointer.getAuthors().contains(upauthor1)) {
found = true;
System.out.println("Here is the current book:" + pointer.getBook());
System.out.println("Please enter the new Title:");
pointer.setTitle(scan.nextLine());
System.out.println("Please Enter the new ISBN:");
pointer.setISBN(scan.nextLine());
System.out.println("Please Enter the new Author:");
pointer.setAuthors(scan.nextLine());
}
pointer = pointer.next;
}
break;
}
}
}
private static void viewBook() {
Book element = head;
System.out.println();
System.out.println("Printing List");
while (element != null) {
System.out.println(element.getBook());
element = element.next;
}
System.out.println("Book Printing Ended");
System.out.println();
}
private static void deleteBook() {
System.out.println();
System.out.println("1. Delete by Title");
System.out.println("2. Delete by ISBN");
System.out.println("3. Delete by Author(s)");
}
}
正如您所看到的,我将删除方法留空了,因为我不知道如何删除在编译开头插入的特定书籍。
这是我的Book类
public class Book {
private String authors;
private String ISBN;
private String title;
public Book next;
Book(String title, String ISBN, String authors) {
this.title = title;
this.authors = authors;
this.ISBN = ISBN;
}
public String getAuthors() {
return authors;
}
public void setISBN(String ISBN){
this.ISBN = ISBN;
}
public void setTitle(String title){
this.title = title;
}
public void setAuthors(String authors) {
this.authors = authors;
}
public String getISBN() {
return ISBN;
}
public String getTitle() {
return title;
}
public String getBook() {
return "Book [authors=" + authors + ", ISBN=" + ISBN + ", title="+ title + "]";
}
}
答案 0 :(得分:1)
1.定义当前指针的前一个指针。
2.当在while-cycle时更改前一个指针
3.在匹配时更改前一指针的下一个和当前指针的下一个(删除指针是否为头)
private static void deleteBook() {
System.out.println();
System.out.println("1. Delete by Title");
System.out.println("2. Delete by ISBN");
System.out.println("3. Delete by Author(s)");
switch (choice) {
case 1:
System.out.println("Please enter Title of BOOK");
String title = scan.nextLine();
if (head == null) {
System.out.println("List is EMPTY !");
return;
} else {
pointer = head;
//define Previous of the current pointer
Book prePointer = null;
while (pointer != null) {
if (pointer.getTitle().equals(title)) {
//delete book
if(prePointer == null){
//delete head
head = pointer.next;
}else{
prePointer.next = pointer.next;
}
break;
}
pointer = pointer.next;
prePointer = pointer;
}
}
break;
}
}