简体   繁体   English

在链表 (Java) 中插入值时遇到问题?

[英]Having trouble inserting values in Linked List (Java)?


Hi, I'm fairly new to LinkedLists in java and am having trouble inserting values into a linked list.嗨,我对 Java 中的 LinkedLists 还很陌生,并且在将值插入链表时遇到了问题。 The program compiles successfully and I am able to add only a few numbers to the linked list.程序编译成功,我只能向链表添加几个数字。 It would help if anyone gave an explanation as to why this is happening and a solution to fixing it.如果有人解释为什么会发生这种情况以及修复它的解决方案,那将会有所帮助。


class LNode {
    int data;
    LNode next;
    public LNode(int data)
    {
        this.data=data;
    }
}

class linkedList {
    LNode head; // initialize head.

    // methods
    public void append(int data) {

        if(head==null) {
            head= new LNode(data);
            return;
        }
        LNode temp = head;
        while(temp.next!=null) {
            temp=temp.next; 
        }
        temp = new LNode(data);
    }

    public void prepend(int data) {
        LNode temp=new LNode(data);
        temp.next=head;
        head=temp;      
    }

    public void DelteValue(int data) {
        if(head==null)
            return;
        if(head.data==data) {
            head=head.next;
            return;
        }
        LNode current=new LNode(data);
        while(current.next!=null) {
            if(current.next.data==data) {
                current.next=current.next.next;
                return;
            }
            current=current.next;
        }

    }
    public void show() {
        LNode temp= head;
        while(temp.next!=null) {
            System.out.println(temp.data);
            temp=temp.next;
        }
    }

}

public class LinkedLists {

    public static void main(String[] args) {
        linkedList LL=new linkedList();
        LL.append(5);
        LL.append(15);
        LL.append(25);
        LL.prepend(1);
        LL.prepend(12);
        LL.prepend(22);

        LL.show();

    }
}

Please have a look and post a solution if possible?如果可能,请查看并发布解决方案? Thanks a ton.万分感谢。


The logic in your append() method is incorrect.您的append()方法中的逻辑不正确。 After traversing to the end of the list, you need to insert the new node at the last position:遍历到链表末尾后,需要在最后位置插入新节点:

public void append(int data) {

    if (head == null) {
        head = new LNode(data);
        return;
    }

    LNode temp = head;
    while (temp.next != null) {
        temp = temp.next;
    }
    temp.next = new LNode(data);
}

Your prepend() method looks fine.您的prepend()方法看起来不错。 I did not test your delete method, and it may have problems as well.我没有测试你的删除方法,它也可能有问题。 This answer addresses directly what you asked above.这个答案直接解决了你上面提出的问题。

It's in your append method:它在您的 append 方法中:

LNode temp = head;
while(temp.next!=null) {
   temp=temp.next; 
}
temp = new LNode(data);

When the while loop finishes, temp is pointing to the LAST thing in your list.当 while 循环结束时, temp 指向列表中的最后一个内容。 But then you throw it out with the assignment on the next line.但是随后您将其与下一行的分配一起扔掉。 It should be它应该是

temp.next = new LNode(data);

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

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