NewbieJava NewbieJava - 1 year ago 56
Java Question

Java Linked List Understanding Methods

I am trying to understand methods in Linked Lists in Java, but I still have some problems.

So I start with the class 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.

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. Then I'd put the pointer of my certain element, e.g. 4 to my new element which I want to insert. 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.

Answer Source

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 
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download