我很困惑,因为我想在现有元素之后添加元素并在链表中删除。
我正在创建方法调用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 )
{
}
}
答案 0 :(得分:0)
为什么不使用java.util.LinkedList
?
因此,您需要创建一个以String bookName
和int millionsSold
作为类型来存储图书信息的新类。您还需要覆盖新创建的类的java.lang.equals(Object o)
方法,如下所示:
public boolean equals(Object o) {
if (o instanceof MyClass) {
if (((MyClass) bookName).equals(o.bookName)) {
return true;
}
}
return false;
}
现在你可以使用:
MyClass insertAfter = ...
myList.add(new MyClass("Book title", 10), myList.firstIndexOf(insertAfter) + 1);
答案 1 :(得分:0)
为什么不使用LinkedList from java API? 如果需要在中间的节点之后添加/删除,则链表不是最佳结构。它是FIFO队列的不错选择。
关联列表:
每个节点(链接)都有对下一个节点(链接)的引用。 [在以前的节点的双链表中。]列表本身包含对第一个节点的引用(在某些情况下也包含对最后一个节点的引用)。
在开头插入:
最后插入
在x:
之后插入在节点后添加
public void addAfterNode(Node n, Node newNode) {
if(n.equals(this)) {
newNode.setNext(this.next);
this.next = newNode;
return this;
} else {
if(this.hasNext()){
return this.next.addAfterNode(n, newNode);
}else {
return null;
}
}
}
在索引后添加:
public void addAfterIndex(int n, Node newNode) {
return this.addAfterIndex(n, newNode, 0);
}
private void addAfterIndex(int n, Node newNode, int curPos){
if(n == curPos) {
newNode.setNext(this.next);
this.next = newNode;
return this;
} else {
if(this.hasNext()){
return this.next.addAfterIndex(n, newNode, curPos++);
}else {
return null;
}
}
}
在元素后添加:
public void addAfterElement(YourClass n, YourClass newElement) {
if(this.element.equals(n)) {
Node newNode = new Node(newElement);
newNode.setNext(this.next);
this.next = newNode;
return this;
} else {
if(this.hasNext()){
return this.next.addAfterelement(n, newElement);
}else {
return null;
}
}
}
删除工作方式相同。
我建议你使用java中的LinkedList。它实施得很好,并且有很好的文档。创建自己的列表仅对教育目的有用。