Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. // make this list generic
  2. public class DoublyLinkedList<ValueType> {
  3. // member variables
  4. private Node head;
  5. private Node tail;
  6. private int size;
  7.  
  8. // constructor
  9. public DoublyLinkedList() {
  10. // create sentinel nodes: head, tail
  11. head = new Node(null, null, null);
  12. tail = new Node(null, null, null);
  13.  
  14. // link together
  15. head.setNext(tail);
  16. tail.setPrev(head);
  17.  
  18. // set size to zero nodes
  19. size = 0;
  20. }
  21.  
  22. // insertAtFront method
  23. public void insertAtFront(ValueType value) {
  24. insertBetween(value, head, head.getNext());
  25. }
  26.  
  27. // private helper method to insert between two nodes
  28. private void insertBetween(ValueType value, Node predecessor, Node successor) {
  29. // which nodes need to be update?
  30. // predecessor.next must point the new node
  31. // new node's prev must point to predecessor
  32. // new node's next must point to successor
  33. // successor.prev must point to the new node
  34.  
  35. // first, create new node
  36. Node newNode = new Node(value, predecessor, successor);
  37.  
  38. // update predecessor
  39. predecessor.setNext(newNode);
  40.  
  41. // update successor
  42. successor.setPrev(newNode);
  43.  
  44. // since we added a new node, increase the size
  45. size++;
  46. }
  47.  
  48. public void remove(ValueType character) {
  49. if (size == 0) {
  50. // do nothing
  51. }
  52. // checks for size of 1 and makes sure the character entered is in the list.
  53. else if (size == 1) {
  54. if (character.equals(head.getValue())) {
  55. head.setNext(null);
  56. size--;
  57. }
  58. }
  59. // Check if the size is greater than 1 but its the first element of the list and
  60. // will remove it.
  61. else if (character.equals(head.getNext().getValue())) {
  62. head.setNext(head.getNext().getNext());
  63. head.getNext().getNext().setPrev(head);
  64. size--;
  65.  
  66. }
  67. // verifies and removes the rest of the specified values from the list.
  68. else {
  69. Node temp = head.getNext();
  70. if (character.equals(head.getNext().getValue())) {
  71. for (; temp.getValue() != character; temp = temp.getNext()) {
  72. System.out.println(temp);
  73. }
  74. temp.getPrev().setNext(temp.getNext());
  75. temp.getNext().setPrev(temp.getPrev());
  76. size--;
  77. }
  78. }
  79. }
  80.  
  81. // size method
  82. public int size() {
  83. return size;
  84. }
  85.  
  86. // toString method
  87. public String toString() {
  88. // string to return
  89. String ret = "";
  90.  
  91. // iterate through values, adding each to the string
  92. for (Node temp = head.getNext(); temp != tail; temp = temp.getNext()) {
  93. ret += (temp.getValue() + " ");
  94. }
  95.  
  96. // return constructed string
  97. return ret;
  98. }
  99.  
  100. // internal node class
  101. private class Node {
  102. private ValueType value;
  103. private Node next;
  104. private Node prev;
  105.  
  106. public Node(ValueType value, Node prev, Node next) {
  107. this.value = value;
  108. this.next = next;
  109. this.prev = prev;
  110. }
  111.  
  112. public ValueType getValue() {
  113. return value;
  114. }
  115.  
  116. public Node getNext() {
  117. return next;
  118. }
  119.  
  120. public void setNext(Node n) {
  121. next = n;
  122. }
  123.  
  124. public Node getPrev() {
  125. return prev;
  126. }
  127.  
  128. public void setPrev(Node p) {
  129. prev = p;
  130. }
  131.  
  132. public String toString() {
  133. return "" + value;
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement