BT93
BT93

Reputation: 339

LinkedList remove at index java

I've made a remove method from scratch that removes a Node from a linked list at a specified index.

It's not removing the correct Node. I've tried to step through with the debugger in eclipse but couldn't catch the problem.

Each Node contains a token. I have included the Token class, Node class. I have written my methods in the List class and included a Test class.

The remove method is currently removing the node next to the specified index. How can I get this to work? My apologies for the long post.

public class thelist{

    public Node head;

    public List() {
        head = null;
    }

    public Node remove(int index) {
        Node node= head;
        for (int i = 0; i < index; i++) {
            node= node.next;
        }
        node.next = node.next.next;
        return node;
    }

Upvotes: 0

Views: 15256

Answers (4)

This could be another solution.

   public E remove(int index) {
            if (index >= size || index < 0) {
                throw new IndexOutOfBoundsException("Index: " + index + ", Size " + index);
            }
    
            Node current = head;
    
            if (index == 0) {
                size--;
                head = current.next;
                return (E) current.element;
            }
    
            Node temp = null;
            for (int i = 1; i <= index; i++) {
                temp = current;
                current = current.next;
            }
    
            temp.next = current.next;
            size--;
    
            return (E) current.element;
        }

Test cases:

    @Test
    void removeByIndex() {
        LinkedList<String> list = new LinkedList<>();
        list.add("a");
        String element = list.remove(0);
        Assertions.assertEquals("a",element);
        Assertions.assertEquals(0,list.size());
    
        list = new LinkedList<>();
        list.add("a");
        list.add("b");
        element = list.remove(1);
        Assertions.assertEquals("b",element);
        Assertions.assertEquals("a",list.get(0));
        Assertions.assertEquals(1,list.size());
    
        list = new LinkedList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        element = list.remove(1);
        Assertions.assertEquals("b",element);
        Assertions.assertEquals("a",list.get(0));
        Assertions.assertEquals("c",list.get(1));
        Assertions.assertEquals(2,list.size());
    }

Upvotes: 0

Saravana Moorthy
Saravana Moorthy

Reputation: 1

Remove by index

public boolean remove(int index) {
     
    // if the index is out of range, exit
    if(index>size|| index <=0){
        return false;
    }
    if (index == 1 ){
        head = head.next;
    }

    Node crunchifyCurrent = head;
    if (head != null) {
        for (int i = 0; i < index-2; i++) {
            crunchifyCurrent = crunchifyCurrent.next;
        }
            if (crunchifyCurrent.next == null){
                return false;
            }else{
        
        crunchifyCurrent.next = crunchifyCurrent.next.next;
            }
        // decrement the number of elements variable
        size--;
        return true;

    }
    return false;
}

Upvotes: 0

na -
na -

Reputation: 1

   public void Atposition(E pos){
    Node<E> curr = head, prev = null;
    if(curr!=null&&curr.getElement()==pos){
    curr=head;
    head=head.getNext();
    size--;
    return;
}while(curr!=null&&curr.getElement()!=pos){
    prev = curr;
    curr=curr.getNext();
}
if(curr==null){
    System.out.print("the list is empty");
    return;
}
    prev.next=curr.next;
}

Upvotes: -1

EmptyArsenal
EmptyArsenal

Reputation: 7464

The problem is that once you get to the correct index, you're removing the NEXT node, not the one at the index. Once you find the correct node, you can to set ref.previous.next to ref.next; thus, cutting out ref.

public Token remove(int index) {
    if (index<0 || index >=size()) {
        throw new IndexOutOfBoundsException();
    }
    Node ref = head;
    for (int i = 0; i < index; i++) {
        ref = ref.next;
    }
    if (index == 0) {
        head = ref.next;
    } else {
        ref.previous.next = ref.next;
    }
    size--;
    return ref.getObject();
}

Upvotes: 4

Related Questions