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:
public MyCircularLinkedList<e> OddSubList(){
MyCircularLinkedList<e> sublist = new MyCircularLinkedList<>();
if (!isEmpty()) {
Node<e> temp = current.next;
do {
if (temp.element % 2 !=0)
sublist.add(temp.element);
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;
}
}