Guest User

Untitled

a guest
May 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. /**
  2. * Example of recursion in Java
  3. */
  4.  
  5. public class recursionExample
  6. {
  7.  
  8. /**
  9. * Main Method
  10. */
  11. public static void main(String[] args)
  12. {
  13. MyLinkedList list = new MyLinkedList();
  14. Node node = new Node();
  15.  
  16.  
  17. list.display();
  18. list.add(5);
  19. list.add(10);
  20. list.add(15);
  21.  
  22. System.out.print("Size using MyLinkedList's getSize() method: \n" + list.getSize());
  23. list.display();
  24.  
  25. }
  26.  
  27. /**
  28. * Recusive Method for detecting number of nodes in a list
  29. */
  30. public int numberOfNodes(MyLinkedList l)
  31. {
  32. //store number of nodes
  33. int result = 0;
  34.  
  35. //base case
  36. if(l == null)
  37. {
  38. return result + 1;
  39. }
  40.  
  41. else
  42. {
  43. result++;
  44. return numberOfNodes(l.link);
  45. }
  46. }
  47.  
  48. }
Add Comment
Please, Sign In to add comment