Click here to Skip to main content
15,357,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
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;
}

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

1 solution

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.
   

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