既存の要素の後に要素を追加し、リンクされたリストで削除する方法は混乱します。既存の要素の後に要素を追加してリンクされたリストで削除する方法[Java]
私はメソッドコールaddAfter
とremoveLinkAfter
を作成しています。 run1とrun2は、次のコードの私の予想される結果です。 「addAfter」と「removeLinkAfter」メソッドの解決方法を教えてください。 私のコードが非常に長い場合は申し訳ありません。
package linkedlist1;
public class Link {
// Set to public so getters and setters aren't needed
public String bookName;
public int millionsSold;
// This is the next pointer for my Link List
// This is important so I have a reference to the Link that was created before it
// Always NULL until it is connected to other links
public Link next;
// Constructor
public Link (String bookName, int millionsSold){
this.bookName = bookName;
this.millionsSold = millionsSold;
}
public void display(){
System.out.println(bookName + " , " + millionsSold + "000,000 Sold");
}
public String toString(){
return bookName;
}
public static void main(String[] args){
LinkList theLinkedList = new LinkList();
theLinkedList.insertFirstLink("Lord of The Rings", 500);
theLinkedList.insertFirstLink("Tale of the DS Students", 1);
theLinkedList.insertFirstLink("Harry Potter", 100);
theLinkedList.insertFirstLink("Buku Sekolah", 10);
theLinkedList.display();
System.out.println ("Take a peek at value of first in LinkedList: " + theLinkedList.firstLink + "\n");
theLinkedList.find("Tale of the DS Students");
//Removes the first book in the linked list
theLinkedList.removeFirst();
theLinkedList.display();
//Removed a particular book
theLinkedList.removeLink("Harry Potter");
theLinkedList.display();
//add after a particular book
theLinkedList.addAfter("Harry Potter", "Zati Biography", 10);
theLinkedList.addAfter("Zati Biography", "Adam Aymar Story", 90);
// remove after a particular book
// theLinkedList.removeLinkAfter("Buku Sekolah");
//theLinkedList.removeLinkAfter("Lord of The Rings");
}
}
class LinkList{
// Reference to the first Link in list
public Link firstLink;
LinkList(){
// First Link is always starts as NULL
firstLink = null;
}
// Returns TRUE if LinkList
public boolean isEmpty(){
return (firstLink == null);
}
// Insert a new Link
public void insertFirstLink (String bookName, int millionsSold){
Link newLink = new Link(bookName, millionsSold); // I'm the new guy in town
// Connects the firstLink (head in town) to the newLink (new guy in town)
newLink.next = firstLink;
firstLink = newLink;
}
public Link removeFirst(){
Link linkReference = firstLink;
if (!isEmpty()){
// Removes the Link from the List
firstLink = firstLink.next;
}
else{
System.out.println("Empty LinkedList");
}
return linkReference;
}
// Display/Traverse Link List
public void display(){
Link theLink = firstLink;
// As long as you have yet to reach the end, display Linked List
while (theLink != null){
theLink.display();
System.out.println("Next Link: " + theLink.next);
theLink = theLink.next;
System.out.println();
}
}
// Searching for a particular data in Linked List
public Link find(String bookName){
Link theLink = firstLink;
if (!isEmpty()){
// If not empty, continue searching for bookname
// If we reach end of link list, but did not found a match, return null
// If we found a match, get the value of next
while (theLink.bookName != bookName)
{
if (theLink.next == null)
{
return null;
}
else
{
theLink = theLink.next;
}
}
}
else
{
System.out.println("Empty Linked List");
}
return theLink;
}
public Link removeLink (String bookName){
Link currentLink = firstLink;
Link previousLink = firstLink;
// Keep searching as long as there is no match
while(currentLink.bookName != bookName){
//Checks if the link is the end
if (currentLink.next == null){
// Bookname not found, so break/leave the method
return null;
}
else{
previousLink = currentLink;
currentLink = currentLink.next;
}
}
if (currentLink == firstLink){
firstLink = firstLink.next;
}
else{
System.out.println("Found a match!");
System.out.println("currentLink: " + currentLink);
System.out.println("firstLink: " + firstLink);
previousLink.next = currentLink.next;
}
return currentLink;
}
public void addAfter(String bookName, String bookAfter, int millionsSold)
{
}
}