Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1.  
  2. /*
  3. * addAtIndex( ):
  4. index < 0 : do nothing.
  5. index > size: add to end
  6. *
  7. */
  8. public void addAtIndex(int index, E data) {
  9. if (index < 0)
  10. return;
  11. if (index > size())
  12. add(data);
  13. if (data != null)
  14. head = addAtIndex(head, index, data);
  15. }
  16.  
  17. private LLNode<E> addAtIndex(LLNode<E> t, int index, E data) {
  18. LLNode<E> n;
  19. if (index == 0) {
  20. n = new LLNode<E>(data); // make new node
  21. n.setNext(t); // point to current node
  22. t = n; // check
  23. }
  24.  
  25. else {
  26. t.setNext(addAtIndex(t.getNext(), index-1, data));
  27. }
  28.  
  29. return t;
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement