简体   繁体   English

在java中实现链表的问题

[英]trouble implementing a linked list in java

I tried to implement a linked list in java using the code below:我尝试使用下面的代码在java中实现一个链表:

    class Linkedlist<T>{

    private node head, tail;

    public Linkedlist(){
        head = new node(null);
        tail = head;
    }

    public boolean insert(T value){
        if(head.getValue() == null){
            this.head.setValue(value);
            head.setNext(null);
            return true;
        }
        node insertNode = new node(value);
        tail.setNext(insertNode);
        tail = insertNode;
        return true;
    }
    public boolean insert(T value, int index)  {

        if ( sizeOfList() == index + 1 ) return false;
        node temp = this.head.getNext();
        node prvtmp = this.head;
        for (int i = 0; i <= index; i++) {
            temp = temp.getNext();
            prvtmp = temp;
            System.out.println("for loop");
        }
        node insertNode = new node(value);
        System.out.println("node created");
        prvtmp.setNext(insertNode);
        insertNode.setNext(temp);
        System.out.println("temps");
        return true;
    }
    public int sizeOfList(){

        int size = 0;
        node temp = this.head;
        while(temp.getNext() != null){
            temp = temp.getNext();
            size++;
        }
        return size;
    }
    public String[] rtrnList(){
        node temp = this.head;
        int listSize = sizeOfList();
        String[] resualt = new String[listSize + 1];
        for (int i = 0;i <= listSize;i++){
            resualt[i] = String.valueOf(temp.getValue());
            temp = temp.getNext();
        }
        return resualt;
    }
}

Class node:类节点:

public class node<T> {

    private  T value;
    private node next;

    public node(T value){
        this.value = value;
        this.next = null;
    }
    public void setValue(T value) {
        this.value = value;
    }

    public void setNext(node next) {
        this.next = next;
    }

    public T getValue() {
        return value;
    }

    public node getNext() {
        return next;
    }


}

When I try to run the insert method with one argument it works fine, but when I run it with two arguments:当我尝试使用一个参数运行insert方法时,它工作正常,但是当我使用两个参数运行它时:

import java.util.Arrays;
public class main {
    public static void main(String[] args){
        Linkedlist list = new Linkedlist();
        list.insert(2);
        list.insert(2);
        list.insert(2);
        list.insert(2);
        list.insert(2);
        list.insert(2);
        list.insert(11, 3);

        System.out.println(Arrays.toString(list.rtrnList()));
        System.out.println("finished");
    }
}

The program doesn't reach the line that prints finished, but if we delete the line: list.insert(11, 3);程序没有到达打印完成的那一行,但是如果我们删除该行: list.insert(11, 3); Then the program works just fine.然后程序运行得很好。

output:输出:

for loop
for loop
for loop
for loop
node created
finish insert

The problems is inside the for loop of your insert(T value, int index) method when you are updating the value of prvtm and temp.当您更新 prvtm 和 temp 的值时,问题出在 insert(T value, int index) 方法的 for 循环内。 In your code you are first setting the value of temp to temp.getnext() and as the temp is already changed setting prvtmp to temp creating a loop which is why when you are trying to print the list it's getting into an infinite loop while calculating the size of the list.在您的代码中,您首先将 temp 的值设置为 temp.getnext() 并且由于 temp 已经更改将 prvtmp 设置为 temp 创建一个循环这就是为什么当您尝试打印列表时它在计算时进入无限循环列表的大小。

Just put the prvtmp = temp line before temp = temp.getNext() like following code snippet.只需将 prvtmp = temp 行放在 temp = temp.getNext() 之前,如下面的代码片段。 This will solve the problem.这将解决问题。

public boolean insert(T value, int index)  { // 2 2 2 2 2 2

    if ( sizeOfList() == index + 1 ) return false;
    node temp = this.head.getNext();
    node prvtmp = this.head;
    for (int i = 0; i <= index; i++) {
        prvtmp = temp;
        temp = temp.getNext();
        System.out.println("for loop");
    }
    node insertNode = new node(value);
    System.out.println("node created");
    prvtmp.setNext(insertNode);
    insertNode.setNext(temp);
    System.out.println("temps");
    return true;
}

Also if you are trying to insert in the given index update the condition of the for loop from:此外,如果您尝试在给定的索引中插入更新 for 循环的条件:

i <= index to i < index - 1

Happy coding!快乐编码!

while inserting there is a cyclic refrence made , you can use this code for above purpose插入时会产生循环引用,您可以将此代码用于上述目的

public boolean insert(T value, int index)  {


    if(index > sizeOfList())
    {
        return false;
    }
    else if(index == sizeOfList())
    {
        if(insert(value))
            return true;
        else
            return false;
    }
    else
    {
        node previous = this.head;

        for (int i = 1; i <= index; i++) {
            if(i==index-1)
            {
                node newnode = new node(value);

                newnode.setNext(previous.getNext());
                previous.setNext(newnode);
                break;
            }
            previous = previous.getNext();

        }
    }

    return true;
}

Output输出

[2, 2, 11, 2, 2, 2, 2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM