15,850,685 members
See more:
I am working on a circular linked list and I have try to create a method that will read the elements of the list and create a sublist of the elements positioned in odd indexes.
for example a list has {(10,10),(20,20),(30,30),(50,50),(40,40)}
the method should return {(20,20),(50,50)}

The method I tried gives me an error when I try to check if temp.element when divided by two its reminder is 0 or not. I tried implementing an index function but that didn't work either.
What is the best method I can use to return and odd sublist using a circular linked list
I am working with pairs so I added the pair class I am using.

What I have tried:

I tried the following method:
if (!isEmpty()) {
Node<e> temp = current.next;
do {
if (temp.element % 2 !=0)
temp = temp.next;
} while(temp!=current);
}
return sublist;
}

public class Pair implements Comparable<pair>{
int x, y;

public Pair(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public String toString() {
return "(" + x + "," + y + ")";
}

@Override
public int compareTo(Pair o) {
if (this.x > o.x)
return 1;
if (this.x < o.x)
return -1;
else
return 0;
}

}
Posted
Updated 13-Nov-21 1:38am

Solution 1

Firslt your element items contain two values, so you cannot divide it by any number.
Secondly, you are supposed to be finding the elements at index 1,3,5 etc, so dividing the content by 2 has no meaning.

You just need to set an index counter and as you traverse the list, extract each item when the index is an odd number.

Top Experts
Last 24hrsThis month
 Pete O'Hanlon 70 M-Badger 60 OriginalGriff 55 Richard MacCutchan 30 merano99 10
 Richard MacCutchan 95 OriginalGriff 95 k5054 70 Pete O'Hanlon 70 M-Badger 60

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