I have this code:
static int countStu = 0;
public static int countStudent(Node<Student> lst) {
// pre : true
// post : res = number of students in list
if (lst != null) {
countStu++;
countStudent(lst.getNext());
}
return countStu;
}
The problem with this method is I must declare countStu
outside the countStudent()
method, which is not good in the case when I want to call countStudent()
twice, it will make the returned value doubles. How do I solve this problem and able to call countStudent()
unlimited times with correct results?