some_cs_student
some_cs_student

Reputation: 21

How to change an element in a linked list?

What is the code that goes into: void changeElem(String oldStr, String newStr)?

I have a driver's class with:

public static void main(String[] args) {
    // create empty list
    SimpleLinkedList list = new SimpleLinkedList(null);

    // add some elements in the list in
    // reverse order so that list is sorted
    list.addElement("W");
    list.addElement("T");
    list.addElement("P");
    list.addElement("L");
    list.addElement("K");
    list.addElement("B");
    list.printList();

    System.out.println("change W to X");
    list.changeElem("W", "X");
}

I am supposed to make a new method called “changeElem” in another class where void changeElem(String oldStr, String newStr) but I am not sure how.

Upvotes: 0

Views: 3086

Answers (3)

Bojan Krkic
Bojan Krkic

Reputation: 477

The answer by manikanta is right; however, changeElement(List list, String string, String replacement) requires you to pass the List as a parameter, which means you can't use it in the context list.changeElement("W", "X").

I'm assuming SimpleLinkedList is a Java class that you have implemented.

To use the method as list.changeElement("W", "X") instead of changeElement(list, "W", "X"), you need to add the method changeElement to the SimpleLinkedList class (which I think is actually your question).

Since actual implementation of the class isn't known, something like this:

public class SimpleLinkedList {
    public void add(String str) {
        //This should be an existing method, for example
    }

    //Assuming this is the implementation
    private class Node {
        String data;
        Node nextNode;
    }

    private Node firstNode; //Should be set/unset in your add/remove method

    //New method. To keep the order of your list, iterate and replace individually
    public void changeElement(String str, String replacement) {
        for (Node n = firstNode; n != null; n = n.nextNode) {
            if (str.equals(n.data)) {
                n.data = replacement;
                break; //If you want to replace only one instance
            }
        }
    }
}

Also, when in doubt: Look at Java's source code. What you basically want to do is find the place where str is and then replace it.

That's almost the Java LinkedList#indexOf method, except instead of returning index when you find str, you're replacing it. Java's LinkedList#indexOf() implementation.

Upvotes: 1

Adam Bickford
Adam Bickford

Reputation: 1386

Assuming your SimpleLinkedList implements the List interface and is at least Java 8:

private static void changeElement(
    List<String> list, String string, String replacement) {
    list.replaceAll(s -> s.equals(string) ? replacement : s);
}

Of course you could call this 'Util' method from your SimpleLinkedList passing itself as the first argument.

Upvotes: 0

manikanta nvsr
manikanta nvsr

Reputation: 567

We just have to add the replacement string at your intended location and then we should remove the unwanted string.

public static void main(String[] args) {
    /* // create empty list
     * LinkedList list = new LinkedList(null);
     */
    LinkedList listLinked = new LinkedList<String>();

    // add some elements in the list in
    // reverse order so that list is sorted
    listLinked.add("W");
    listLinked.add("T");
    listLinked.add("P");
    listLinked.add("L");
    listLinked.add("K");
    listLinked.add("B");

    changeElement(listLinked, "W", "X");
    System.out.println(listLinked);
}
private static void changeElement(
        List list, String string, String replacement) {
    if (list.contains(string)) {
        // adding the replacement
        list.add(list.indexOf(string), replacement);
        // removing the unwanted
        list.remove(string);
    } else {
        System.out.println("Element not found to replace");
    }
}

Upvotes: 0

Related Questions