Click here to Skip to main content
15,312,747 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on expanding my circular linked list, I implemented a few methods that will add and delete nodes from a list. I want to create a method that will Increment all the lists’ values by a given Value, the method should return the following:

before increment:

List1 = [(10,10), (50,50), (50,50)]

after increment:

List1.IncrementList(p10)

List1 = [(20,20), (60,60), (60,60)]

What I have tried:

I tried implementing the following method:

public void IncrementList(E e) {
if (!isEmpty()) {
Node temp = current;
do {
if (temp.element.compareTo(e)==0)
temp.element = temp.element + e ;
} while(temp!=current);
}
}

the temp.element + e line is giving me an error due to the operator how can I fix this, and how can I implement the increment method correctly.
Posted
Updated 11-Nov-21 21:46pm

1 solution

The method element.compareTo is specifcally for Strings of text, not integer values. The correct code is
Java
if (temp.element == 0) // although why do you want this?
temp.element = temp.element + e ; // this should be all you need

But to be honest even that does not make a lot of sense. What type is E that is passed in to this method? All you really need is something like:
Java
public void IncrementList(int increment) {
    if (!isEmpty()) {
        Node temp = current;
        do {
            temp.element = temp.element + increment;
            temp = temp.next; // point to next node in the list
            } while(temp!=current);
    }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900