为什么我无法将元素插入到我的链表实现中?

huangapple go评论78阅读模式
英文:

Why am I not able to insert elements into my linked-list implementation?

问题

我正在使用Java实现链表。特别是在进行插入操作时,如果给定的索引超过链表的长度,我必须将值追加到链表中。尽管我已经为此编写了代码,但情况并不是这样发生。

为了调试这个问题,我添加了打印链表的代码行。我可以在插入函数中的条件if(index >= this.length())之前打印列表,但不能在条件内部的代码行中打印链表。

package com.learning.algorithms.LinkedList;

public class Node {
    int data;
    Node next;

    public Node() {
    }

    public Node(int i) {
        this.data = i;
        this.next = null;
    }
}
package com.learning.algorithms.LinkedList;

public class LinkedList {
    Node head;
    Node tail;

    public LinkedList() {
        this.head = new Node();
        this.tail = this.head;
    }

    public LinkedList(int value) {
        this.head = new Node();
        this.head.data = value;
        this.head.next = null;
        this.tail = this.head;
    }

    public LinkedList append(int value) {
        Node newNode = new Node(value);
        this.tail.next = newNode;
        this.tail = newNode;

        return this;
    }

    public void printList() {
        Node useNode = this.head;

        while (useNode != null) {
            System.out.println(useNode.data);
            useNode = useNode.next;
        }

        //print(useNode);
    }

    private void print(Node useNode) {
        if (useNode != null) {
            System.out.println(useNode.data);
            print(useNode.next);
        }
    }

    public LinkedList prepend(int i) {
        Node newNode = new Node(i);
        newNode.next = this.head;
        this.head = newNode;
        return this;
    }

    public LinkedList insert(int index, int value) {
        if (index == 0) {
            this.prepend(value);
            return this;
        }

        this.printList();
        if (index >= this.length()) {
            System.out.println("inside");
            this.printList();
            this.append(value);
            return this;
        }

        Node nodeJustBeforeGivenIndex = this.head;

        // getting node just before given index using while loop
        while (index > 1) {
            nodeJustBeforeGivenIndex = nodeJustBeforeGivenIndex.next;
            index--;
        }

        // make an insert
        Node newNode = new Node(value);
        newNode.next = nodeJustBeforeGivenIndex.next;
        nodeJustBeforeGivenIndex.next = newNode;

        return this;
    }

    private int length() {
        int counnt = 0;

        if (this.head != null) {
            while (this.head != null) {
                this.head = this.head.next;
                counnt++;
            }
        }

        return counnt;
    }
}
package com.learning.algorithms.LinkedList;

public class LinkedListImplementation {
    public static void main(String[] args) {
        LinkedList list = new LinkedList(10);

        list.append(5);
        list.append(16);
        list.prepend(1);
        list.insert(0, 15);
        list.insert(10, 222);

        list.printList();
    }
}

运行此实现类的控制台输出:

15
1
10
5
16
inside
英文:

I am doing linkedlist implementation in java. Particularly, while doing insert operation, if the given index is more than the length of the linkedlist, I have to append the value to the linkedlist. Though I have written code for the same, it is not happening that way.

In order to debug this, I added lines for printing linkedlist. I could print the list just before the condition if(index >= this.length()) in the insert function but not able to print the linkedlist in the line inside the condition.

package com.learning.algorithms.LinkedList;
public class Node {
int data;
Node next;
public Node() {
}
public Node(int i) {
this.data = i;
this.next = null;
}
}
package com.learning.algorithms.LinkedList;
public class LinkedList {
Node head;
Node tail;
public LinkedList() {
this.head = new Node();
this.tail = this.head;
}
public LinkedList(int value) {
this.head = new Node();
this.head.data = value;
this.head.next = null;
this.tail = this.head;
}
public LinkedList append(int value) {
Node newNode = new Node(value);
this.tail.next = newNode;
this.tail = newNode;
return this;
}
public void printList()	{
Node useNode = this.head;
while(useNode!=null)
{
System.out.println(useNode.data);
useNode = useNode.next;
}
//print(useNode);
}
private void print(Node useNode) {
if(useNode!=null)
{
System.out.println(useNode.data);
print(useNode.next);
}		
}
public LinkedList prepend(int i) {
Node newNode = new Node(i);
newNode.next = this.head;
this.head  = newNode;
return this;
}
public LinkedList insert(int index, int value) {
if(index == 0)
{
this.prepend(value);
return this;
}
this.printList();
if(index >= this.length())
{
System.out.println("inside");
this.printList();
this.append(value);
return this;
}
Node nodeJustBeforeGivenIndex = this.head;
// getting node just before given index using while loop
while(index>1)
{
nodeJustBeforeGivenIndex = nodeJustBeforeGivenIndex.next;
index--;
}
// make an insert
Node newNode = new Node(value);
newNode.next = nodeJustBeforeGivenIndex.next;
nodeJustBeforeGivenIndex.next = newNode;
return this;
}
private int length() {
int counnt = 0;
if(this.head !=null ) {
while(this.head !=null )
{
this.head = this.head.next;
counnt++;
}
}
return counnt;
}
}
package com.learning.algorithms.LinkedList;
public class LinkedListImplementation {
public static void main(String[] args)
{
LinkedList list = new LinkedList(10);
list.append(5);
list.append(16);
list.prepend(1);
list.insert(0, 15);
list.insert(10, 222);
list.printList();
}
}

Console output for running this implementation class:

15
1
10
5
16
inside

答案1

得分: 1

你不应该在 length 方法内部修改头部值。

这样会修复它:

private int length() {
    int count = 0;

    Node iter = this.head;
    while(iter != null) {
        iter = iter.next;
        count++;
    }
    
    return count;
}

仅注释掉 insert 方法内的两条打印语句,输出将会是:

inside
15
1
10
5
16
222
英文:

You should not modify the head value inside the length method.

This will fix it:

private int length() {
    int counnt = 0;

    Node iter = this.head;
    while(iter !=null )
    {
    	iter = iter.next;
        counnt++;
    }
    
    return counnt;
}

Commenting just the two print stamtents inside insert, the output gets to be:

inside
15
1
10
5
16
222

huangapple
  • 本文由 发表于 2020年4月7日 00:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61064542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定