Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package list;
  2.  
  3. import list.List;
  4. import list.Node;
  5.  
  6. /** An implementation of List USING REFERENCES */
  7.  
  8. public class LinkedList<E> implements List<E> {
  9. int size = 0; //LinkedLists general starting size
  10. Node<E> head;
  11. Node<E> tail;
  12. private Node<E> ref;
  13.  
  14. private void setRef(int index)
  15. {
  16. ref = head.next;
  17. for(int i=0;i<index;i++)
  18. ref = ref.next;
  19. }
  20.  
  21. public LinkedList()
  22. {
  23. head = new Node<E>(null, null, null);
  24. tail = new Node<E>(null, null, head); //new stuff
  25. head.next = tail;
  26. }
  27.  
  28. public void add(E value)
  29. {
  30. Node<E> temp = new Node<E>(value, tail, tail.prev); //creating new Node (needs value and next fields)
  31. tail.prev.next = temp; //putting reference to new Node in temp
  32. tail.prev = temp;
  33. size++; //added element successfully, increment size
  34. }
  35.  
  36. public void add(int index, E value)
  37. {
  38. //Move to position (index-1)
  39. setRef(index);
  40. Node<E> temp = new Node<E>(value, ref, ref.prev); //new node new params
  41. ref.prev.next = temp;
  42. ref.prev = temp;
  43. size++;
  44. }
  45.  
  46. public E get(int index) //getting value in List at positon index
  47. {
  48. setRef(index);
  49. return ref.value; //in this node, we want the field (value)
  50. } //ref variable is gone, gets cleared
  51.  
  52. public E set(int index, E value)
  53. {
  54. setRef(index);
  55. E result = ref.value;
  56. ref.value = value; //asign parameter value from new value by elminating old one
  57. return result;
  58. }
  59.  
  60. public E remove(int index) //gets you to this position
  61. {
  62. setRef(index);
  63. ref.prev.next = ref.next; //asigned from ref.next
  64. ref.next.prev = ref.prev;
  65. size--;
  66. return ref.value;
  67. }
  68.  
  69. public int size()
  70. {
  71. return this.size;
  72.  
  73. }
  74.  
  75. public boolean isEmpty()
  76. {
  77. for (int i=0;i<size;)
  78. if ((head.next == tail))
  79. {
  80. return false;
  81. }
  82. else
  83. {
  84. return false;
  85. }
  86. return true;
  87. }
  88.  
  89. public void clear()
  90. {
  91. for(int i=0;i<size;i++)
  92. remove(i);
  93. size = 0;
  94. }
  95.  
  96. public indexOf(Object)
  97. {
  98. //
  99. }
  100.  
  101. public contains(Object)
  102. {
  103.  
  104. }
  105.  
  106. public toString()
  107. {
  108.  
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement