Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import java.util.NoSuchElementException;
  2. import java.util.*;
  3.  
  4. /**
  5. * A class to represent a linked list of nodes.
  6. */
  7. public class LinkedList<T> implements Iterable<T> {
  8. /** the first node of the list, or null if the list is empty */
  9. private LLNode<T> front;
  10.  
  11. /**
  12. * Creates an initially empty linked list
  13. */
  14. public LinkedList() {
  15. front = null;
  16. }
  17.  
  18. /**
  19. * Returns the first node.
  20. */
  21. protected LLNode<T> getFront() {
  22. return front;
  23. }
  24.  
  25. /**
  26. * Changes the first node.
  27. * @param node the first node of the new linked list
  28. */
  29. protected void setFront(LLNode<T> node) {
  30. this.front = node;
  31. }
  32.  
  33. public ArrayList toArrayList(){
  34. LLNode<T> nodeptr = getFront();
  35. ArrayList<T> list = new ArrayList<T>();
  36. for (int i = 0; i < length(); i++){
  37. list.add(i, nodeptr.getElement());
  38. nodeptr = nodeptr.getNext();
  39. }
  40. return list;
  41. }
  42.  
  43. /**
  44. * Add an element to the front of the linked list
  45. */
  46. public void addToFront(T element) {
  47. setFront(new LLNode<T>(element, getFront()));
  48. }
  49.  
  50. /**
  51. * Return whether the list is empty
  52. * @return true if the list is empty
  53. */
  54. public boolean isEmpty() {
  55. return (getFront() == null);
  56. }
  57.  
  58. /**
  59. * Returns the length of the linked list
  60. * @return the number of nodes in the list
  61. */
  62. public int length() {
  63. int count = 0; // counts number of nodes seen
  64. LLNode<T> nodeptr = getFront();
  65. while (nodeptr != null) {
  66. count++;
  67. nodeptr = nodeptr.getNext();
  68. }
  69. return count;
  70. }
  71.  
  72. /**
  73. * Remove and return the element at the front of the list
  74. * @return the first element of the list
  75. * @throws NoSuchElementException if there is no such element
  76. */
  77. public T removeFromFront() {
  78. if (isEmpty())
  79. throw new NoSuchElementException();
  80. else {
  81. T save = getFront().getElement();
  82. setFront(getFront().getNext());
  83. return save;
  84. }
  85. }
  86.  
  87. /**
  88. * Add an element to the very end of the list
  89. * @param element the element to add to the end of the list
  90. */
  91. public void addToEnd(T element) {
  92. if (isEmpty())
  93. addToFront(element);
  94. else {
  95. LLNode<T> nodeptr = getFront();
  96. // the loop will end with nodeptr looking at the last node in list
  97. while (nodeptr.getNext() != null)
  98. nodeptr = nodeptr.getNext();
  99. nodeptr.setNext(new LLNode<T>(element, null));
  100. }
  101. }
  102.  
  103.  
  104.  
  105. /**
  106. * Required by the Iterable interface.
  107. * @return an LLiterator for the list
  108. */
  109. public LLIterator<T> iterator() {
  110. return new LLIterator<T>(getFront());
  111. }
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement