Guest User

Untitled

a guest
Feb 14th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. class LinkedListNode<E> {
  2.  
  3. private E element;
  4. private LinkedListNode<E> next;
  5.  
  6. public LinkedListNode(E el) {
  7. element = el;
  8. }
  9.  
  10. public LinkedListNode<E> addToEnd(E el) {
  11. LinkedListNode<E> last = this;
  12.  
  13. while (last.next != null) {
  14. last = last.next;
  15. }
  16.  
  17. LinkedListNode<E> node = new LinkedListNode<>(el);
  18. last.next = node;
  19.  
  20. return node;
  21. }
  22.  
  23. public void print() {
  24. LinkedListNode<E> last = this;
  25.  
  26. while (last.next != null) {
  27. System.out.print(String.valueOf(last) + " -> ");
  28. last = last.next;
  29. }
  30. System.out.println(last);
  31. }
  32.  
  33. public E getElement() {
  34. return element;
  35. }
  36.  
  37. public LinkedListNode<E> setElement(E element) {
  38. this.element = element;
  39. return this;
  40. }
  41.  
  42. public LinkedListNode<E> getNext() {
  43. return next;
  44. }
  45.  
  46. public LinkedListNode<E> setNext(LinkedListNode<E> next) {
  47. this.next = next;
  48. return this;
  49. }
  50.  
  51. @Override
  52. public String toString() {
  53. return String.valueOf(element);
  54. }
  55. }
Add Comment
Please, Sign In to add comment