Skip to content
Advertisement

Removing values from a custom LinkedList class

This custom class mimics the functionality of Java’s LinkedList Class except it only takes integers and obviously lacks most of the functionality. For this one method, removeAll(), I am to go through each node for the list and remove all nodes with that value. My problem is that when the first node in the list contains the value to be removed, it then ignores all subsequent nodes that also contain that value. What seems to be the problem? Am I removing the front node the wrong way? For example, [1]->[1]->[1] should return an empty list, but it leaves the front node and I get [1]

edit: it seems to fail to remove the second node instead of the first.

This is the class (Stores ListNodes as a list):

public class LinkedIntList {
    private ListNode front;  // first value in the list

    // post: constructs an empty list
    public LinkedIntList() {
        front = null;
    }

    // post: removes all occurrences of a particular value
    public void removeAll(int value) {
        ListNode current = front; // primes loop
        if (current == null) { // If empty list
            return;
        }
        if (front.data == value) { // If match on first elem
            front = current.next;
            current = current.next;
        }           
        while (current.next != null) { // If next node exists
            if (current.next.data == value) { // If match at next value
                current.next = current.next.next;
            } else { // If not a match
                current = current.next; // increment to next
            }
        }
    }

    // post: appends the given value to the end of the list
    public void add(int value) {
        if (front == null) {
            front = new ListNode(value);
        } else {
            ListNode current = front;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new ListNode(value);
        }
    }

    // Sets a particular index w/ a given value 
    public void set(int index, int value) {
        ListNode current = front;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        current.data = value;
    }
} 

Here is the ListNode class (responsible for a single “node”):

//ListNode is a class for storing a single node of a linked
//list.  This node class is for a list of integer values.

public class ListNode {
    public int data;       // data stored in this node
    public ListNode next;  // link to next node in the list

    // post: constructs a node with data 0 and null link
    public ListNode() {
        this(0, null);
    }

    // post: constructs a node with given data and null link
    public ListNode(int data) {
        this(data, null);
    }

    // post: constructs a node with given data and given link
    public ListNode(int data, ListNode next) {
        this.data = data;
        this.next = next;
    }
}

Advertisement

Answer

The [1] element that actually stays in the list is the second element which becomes the front element in your code:

if (front.data == value) { // If match on first elem
    front = current.next;
    current = current.next;
}

After that you just iterate over the list and remove the matching elements. Replacing the problematic code with this should do the work:

while (front.data == value) { // If match on first elem
    front = front.next;
    if (front == null) {
        return;
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement