Java 使用“插入”和“删除”预定义的链接列表的“合并到”功能
好的,我对mergeTo函数有点问题,它应该把两个链表合并成一个(这个链表和那个链表-合并成这个)。以下是我的mergeTo代码:Java 使用“插入”和“删除”预定义的链接列表的“合并到”功能,java,linked-list,Java,Linked List,好的,我对mergeTo函数有点问题,它应该把两个链表合并成一个(这个链表和那个链表-合并成这个)。以下是我的mergeTo代码: public void mergeTo(WordLinkedList that){ Node current = that.firstnode; int i = 0; while (current != null){ //iterating through all the elements of 'that' list this.
public void mergeTo(WordLinkedList that){
Node current = that.firstnode;
int i = 0;
while (current != null){ //iterating through all the elements of 'that' list
this.insert(current.word);//inserting each successive word into 'this'
that.remove(i); //removing each successive word from 'that'
current = current.next;//moving the pointer forward
i++; //moving the counter forward (for the remove function
}
}
我对该函数的逻辑是迭代该链表的每个元素——将每个连续元素插入“this”列表,然后立即将其删除(使用i作为计数器)
这是我的插入函数:
public void insert(String newword){
Node newNode = new Node(newword);
size++;
Node previous = null;
Node present = firstnode;
while ((present != null)&& (newword.compareTo(present.word) > 0)){
previous = present;
present = present.next;
}
if (present != null && newword.compareTo(present.word)==0){
size--;
return;
}
if ((previous == null)){
firstnode = newNode;
}
else{
previous.next = newNode;
}
newNode.next = present;
}
public String remove (int i){
Node present = firstnode;
Node previous = firstnode;
if (i > this.size || i < 0)
throw new IndexOutOfBoundsException("Hahahahahahaha it's out of bounds!!!!!!!!");
if (i == 0 && firstnode != null){
firstnode = firstnode.next;
}
else{
for (int j = 0; j < i; j++){
present = present.next;
}
for (int k = 0; k < i-1; k++){
previous = previous.next;
}
previous.next = present.next;
present.next = null;
size--;
}
return present.word;
}
String[] words6={"eta","gamma", "zeta"};//to test mergeTo
String[] words7={"alpha","beta","phi"};//to test mergeTo - no common words with words6
listObj1=new WordLinkedList(words6);
listObj2=new WordLinkedList(words7);
listObj1.mergeTo(listObj2);
System.out.println("this list: \n"+listObj1.toString());
System.out.println("size of this list = " + listObj1.getSize());
System.out.println("that list: \n"+listObj2.toString());
System.out.println("size of that list = " + listObj2.getSize());
我的insert函数应该在保持字母顺序的同时将内容插入到我的链表中。我已经测试过了,我认为它可以正常工作。我不相信有什么问题
以下是我的删除功能:
public void insert(String newword){
Node newNode = new Node(newword);
size++;
Node previous = null;
Node present = firstnode;
while ((present != null)&& (newword.compareTo(present.word) > 0)){
previous = present;
present = present.next;
}
if (present != null && newword.compareTo(present.word)==0){
size--;
return;
}
if ((previous == null)){
firstnode = newNode;
}
else{
previous.next = newNode;
}
newNode.next = present;
}
public String remove (int i){
Node present = firstnode;
Node previous = firstnode;
if (i > this.size || i < 0)
throw new IndexOutOfBoundsException("Hahahahahahaha it's out of bounds!!!!!!!!");
if (i == 0 && firstnode != null){
firstnode = firstnode.next;
}
else{
for (int j = 0; j < i; j++){
present = present.next;
}
for (int k = 0; k < i-1; k++){
previous = previous.next;
}
previous.next = present.next;
present.next = null;
size--;
}
return present.word;
}
String[] words6={"eta","gamma", "zeta"};//to test mergeTo
String[] words7={"alpha","beta","phi"};//to test mergeTo - no common words with words6
listObj1=new WordLinkedList(words6);
listObj2=new WordLinkedList(words7);
listObj1.mergeTo(listObj2);
System.out.println("this list: \n"+listObj1.toString());
System.out.println("size of this list = " + listObj1.getSize());
System.out.println("that list: \n"+listObj2.toString());
System.out.println("size of that list = " + listObj2.getSize());
还有我的假设输出:
测试17-合并到
此列表:
阿尔法
贝塔
希腊字母表的第7个字母
伽马射线
phi
泽塔
此列表的大小=6
该清单:
列表是空的
该列表的大小=0
以及我的实际产出:
此列表:
阿尔法
贝塔
希腊字母表的第7个字母
伽马射线
泽塔
此列表的大小=5
该清单:
贝塔
该列表的大小=2好的,因此我找到了解决问题的方法:)。在mergeTo方法中,应该将that.remove(i)更改为that.remove(0)-remove方法每次都删除第一个索引