简体   繁体   English

Java链表理解方法

[英]Java Linked List Understanding Methods

I am trying to understand methods in Linked Lists in Java, but I still have some problems. 我试图理解Java链表中的方法,但仍然存在一些问题。

So I start with the class Element: 因此,我从Element类开始:

class Element {
int val; // can be anything like String etc.
Element next; // Pointer to the next Element

Then I have the class List: 然后我有班级名单:

public class List {

Element head = null; // Beginning of the list (head)

Now to the methods: Look at the comments please. 现在介绍方法:请查看评论。 First I start with a method, which inserts an Element to the beginning of the list. 首先,我从一个方法开始,该方法将一个Element插入列表的开头。

public void insertAtBegin (int x){
    Element n = new Element(); // We create a new Object Element called n
    n.val = x; // We pass the value of x to the object n with the attribute int val (Is that right?)
    n.next = head; // What happens here? 
    head = n; // The new Element n becomes the head
}

The second method inserts an element at the end of the list: 第二种方法在列表的末尾插入一个元素:

public void insertAtEnd(int x){
    Element n = new Element(); 
    n.val = x; 

    if (head == null){ // If the list is empty
        head = n; // we insert the Element n as the first element
    }
    else{   
        Element h = head; // I think this helps as a reference right?
        while (h.next != null){ // While the pointer of the head isn't empty
            h = h.next; // our Element h becomes the next Element added to the list
        }
        h.next = n; // If none of the cases above apply, the Element h ist just added at the end of the list right? 
    }
}

What would the method look like if I now want to insert an element after a certain number? 如果我现在想在某个数字后插入一个元素,该方法将是什么样? Not at the beginning, nor at the end. 不在开始,也没有结束。 In theory I would first of all look if the head is null. 从理论上讲,我将首先查看head是否为空。 Then I'd put the pointer of my certain element, eg 4 to my new element which I want to insert. 然后,将特定元素的指针(例如4)放到要插入的新元素上。 And the pointer of the newly inserted element to the upcoming element. 以及新插入的元素指向即将到来的元素的指针。 But I don't know how to put this in code.. 但是我不知道如何将其放入代码中。

I also have a method which removes the last element of the list and inserts it at the beginning. 我还有一个方法可以删除列表的最后一个元素,并将其插入到开头。 Can someone please comment on how this works as well? 有人可以发表评论吗?

public void lastToBegin(){
    if (head != null && head.next != null){ 
        Element e = head; 
        while(e.next.next != null){
            e = e.next;
        }
        Element h = e.next;
        e.next = null;
        h.next = head;
        head = h;
    }
}

I have more methods, but I'd first of all like to understand the basics. 我有更多方法,但是我首先想了解基础知识。 I appreciate any kind of help, thanks. 我感谢任何帮助,谢谢。

If you want to insert an element at a certain index in the list, it would look very similar to the method you have to insert at the end, except you have to stop at the correct index, not at the end, and you have to be sure to add the 'tail' of the list to the element you're inserting. 如果要在列表中的某个索引处插入元素,则该元素看起来与必须在末尾插入的方法非常相似,不同之处在于必须在正确的索引处而不是末尾停止,并且必须确保将列表的“尾部”添加到您要插入的元素中。

So your code would look something like this: 因此,您的代码将如下所示:

public void insertAtIndex(int x, int index){
    Element n = new Element(); 
    n.val = x; 

    if (head == null){ // If the list is empty
        head = n; // we insert the Element n as the first element
    }
    else{   
        Element h = head; // our current element we are looking at
        int currentIndex = 0;
        while (h.next!=null && currentIndex < index) { // increment until we reach the correct index
            h = h.next; // our Element h becomes the next Element added to the list        
            currentIndex++;
        }
        Element tail = h.next; // store the rest of the list in a temp variable, tail
        h.next = n; // we've reached the right index and/or the end, so add the element n here
        n.next = tail // reattach the tail 
    }
}

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

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