patternjavaModerate
Counting number of nodes between two nodes
Viewed 0 times
nodesnumbercountingtwobetween
Problem
This code counts the number of nodes between any given two nodes in a circular linked list. Any feedback is appreciated:
public class CountNodesBetweenTwoNodes {
Node first;
Node fifth;
private class Node {
Item item;
Node next;
}
public static void main(String[] args) {
CountNodesBetweenTwoNodes countNodes = new CountNodesBetweenTwoNodes();
countNodes.buildCircularList();
int noOfNodes = countNodes.countNodesBetweenTwoNodes(countNodes.getFirst(), countNodes.getFifth());
System.out.println("Number of Nodes is: "+noOfNodes);
}
private Node buildCircularList() {
first = new Node();
first.item = "First";
Node second = new Node();
second.item = "Second";
Node third = new Node();
third.item = "Third";
Node forth = new Node();
forth.item = "Forth";
fifth = new Node();
fifth.item = "Fifth";
first.next = second;
second.next = third;
third.next = forth;
forth.next = fifth;
fifth.next = first;
return first;
}
private int countNodesBetweenTwoNodes(Node firstNode, Node secondNode) {
int count = 0;
while(firstNode.next != secondNode) {
count++;
firstNode = firstNode.next;
}
return count;
}
public Node getFirst() {
return first;
}
public Node getFifth() {
return fifth;
}
}Solution
Your program is really based only of case where you have 5 nodes. This make your code really hard to change if you want to have a "list" of node of a different size.
-
Personally, I find that re-using the first argument in the loop to iterate over the list is not what I would want. I would use a third
-
I would rename the class variable
-
Since we changed the name of the variable, we should change
-
You should always specify a visibility for you class variable. Currently,
-
There is a bug in your class: you made your
- The name of the class should represent a concept not an action,
CountNodesBetweenTwoNodescould be something likeCircularListor something like that. A name that would represent what the class is, not what it does.
-
Personally, I find that re-using the first argument in the loop to iterate over the list is not what I would want. I would use a third
Node variable to let to show that I'm iterating and that my firstNode will not change during this method.private int countNodesBetweenTwoNodes(Node firstNode, Node secondNode) {
int count = 0;
while(firstNode.next != secondNode) {
count++;
firstNode = firstNode.next;
}
return count;
}-
I would rename the class variable
Node fifth for Node last, what if you have more than five nodes ? Would you change your code ? I hope not, so by changing the name to represent that it's the last one, we are good to not limit our-self to five nodes.-
Since we changed the name of the variable, we should change
public Node getFifth() too for public Node getLast().-
You should always specify a visibility for you class variable. Currently,
first and fifth are package visibility, they should be private.-
There is a bug in your class: you made your
countNodesBetweenTwoNodes private. If I want to use your class myself, I could not use this method since I would not be in your class. You don't have this problem since you use the method in your main which is in the class. If you move your main method to another class, your program is not working anymore.Code Snippets
private int countNodesBetweenTwoNodes(Node firstNode, Node secondNode) {
int count = 0;
while(firstNode.next != secondNode) {
count++;
firstNode = firstNode.next;
}
return count;
}Context
StackExchange Code Review Q#43191, answer score: 10
Revisions (0)
No revisions yet.