Guest User

Untitled

a guest
May 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. /**
  2. * Data Structures Hands On 6
  3. * @author Small
  4. */
  5.  
  6. /* Class linkedList */
  7. public class LinkedListWithRecursiveMethod
  8. {
  9. protected Node start;
  10. protected Node end ;
  11. public int size ;
  12.  
  13. /* Constructor */
  14. public LinkedListWithRecursiveMethod()
  15. {
  16. start = null;
  17. end = null;
  18. size = 0;
  19. }
  20. /* Function to check if list is empty */
  21. public boolean isEmpty()
  22. {
  23. return start == null;
  24. }
  25. /* Function to get size of list */
  26. public int getSize()
  27. {
  28. return size;
  29. }
  30. /* Function to insert an element at begining */
  31. public void insertAtStart(int val)
  32. {
  33. Node nptr = new Node(val, null);
  34. size++ ;
  35. if(start == null)
  36. {
  37. start = nptr;
  38. end = start;
  39. }
  40. else
  41. {
  42. nptr.setLink(start);
  43. start = nptr;
  44. }
  45. }
  46. /* Function to insert an element at end */
  47. public void add(int val)
  48. {
  49. Node nptr = new Node(val,null);
  50. size++ ;
  51. if(start == null)
  52. {
  53. start = nptr;
  54. end = start;
  55. }
  56. else
  57. {
  58. end.setLink(nptr);
  59. end = nptr;
  60. }
  61. }
  62.  
  63. /* Function to display elements */
  64. public void printList()
  65. {
  66. System.out.print("\nSingly Linked List = ");
  67. if (size == 0)
  68. {
  69. System.out.print("empty\n");
  70. return;
  71. }
  72. if (start.getLink() == null)
  73. {
  74. System.out.println(start.getData() );
  75. return;
  76. }
  77. Node ptr = start;
  78. System.out.print(start.getData()+ "->");
  79. ptr = start.getLink();
  80. while (ptr.getLink() != null)
  81. {
  82. System.out.print(ptr.getData()+ "->");
  83. ptr = ptr.getLink();
  84. }
  85. System.out.print(ptr.getData()+ "\n");
  86. }
  87.  
  88. public class Node
  89. {
  90. protected int data;
  91. protected Node link;
  92.  
  93. /* Constructor */
  94. public Node()
  95. {
  96. link = null;
  97. data = 0;
  98. }
  99. /* Constructor */
  100. public Node(int d,Node n)
  101. {
  102. data = d;
  103. link = n;
  104. }
  105. /* Function to set link to next Node */
  106. public void setLink(Node n)
  107. {
  108. link = n;
  109. }
  110. /* Function to set data to current Node */
  111. public void setData(int d)
  112. {
  113. data = d;
  114. }
  115. /* Function to get link to next node */
  116. public Node getLink()
  117. {
  118. return link;
  119. }
  120. /* Function to get data from current Node */
  121. public int getData()
  122. {
  123. return data;
  124. }
  125. }
  126.  
  127. public static void main(String[] args)
  128. {
  129. LinkedListWithRecursiveMethod list = new LinkedListWithRecursiveMethod();
  130.  
  131. list.add(5);
  132. list.add(10);
  133. list.add(15);
  134.  
  135. System.out.println("Contents of list: " + list.printList());
  136. System.out.print("Size using non-recursive method: \n" + list.getSize());
  137.  
  138.  
  139. }
  140. }
Add Comment
Please, Sign In to add comment