Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. public static class DLLNode<E> {
  2. protected E element;
  3. protected DLLNode<E> pred, succ;
  4.  
  5. public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  6. this.element = elem;
  7. this.pred = pred;
  8. this.succ = succ;
  9. }
  10.  
  11. @Override
  12. public String toString() {
  13. return element.toString();
  14. }
  15. }
  16.  
  17. public static class DLL<E> {
  18. private DLLNode<E> first, last;
  19.  
  20. public DLL() {
  21. // Construct an empty SLL
  22. this.first = null;
  23. this.last = null;
  24. }
  25.  
  26. public void deleteList() {
  27. first = null;
  28. last = null;
  29. }
  30.  
  31. public int length() {
  32. int ret;
  33. if (first != null) {
  34. DLLNode<E> tmp = first;
  35. ret = 1;
  36. while (tmp.succ != null) {
  37. tmp = tmp.succ;
  38. ret++;
  39. }
  40. return ret;
  41. } else
  42. return 0;
  43.  
  44. }
  45.  
  46. public DLLNode<E> find(E o) {
  47. if (first != null) {
  48. DLLNode<E> tmp = first;
  49. while (tmp.element != o && tmp.succ != null)
  50. tmp = tmp.succ;
  51. if (tmp.element == o) {
  52. return tmp;
  53. } else {
  54. System.out.println("Elementot ne postoi vo listata");
  55. }
  56. } else {
  57. System.out.println("Listata e prazna");
  58. }
  59. return first;
  60. }
  61.  
  62. public void insertFirst(E o) {
  63. DLLNode<E> ins = new DLLNode<E>(o, null, first);
  64. if (first == null)
  65. last = ins;
  66. else
  67. first.pred = ins;
  68. first = ins;
  69. }
  70.  
  71. public void insertLast(E o) {
  72. if (first == null)
  73. insertFirst(o);
  74. else {
  75. DLLNode<E> ins = new DLLNode<E>(o, last, null);
  76. last.succ = ins;
  77. last = ins;
  78. }
  79. }
  80.  
  81. public void insertAfter(E o, DLLNode<E> after) {
  82. if(after==last){
  83. insertLast(o);
  84. return;
  85. }
  86. DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  87. after.succ.pred = ins;
  88. after.succ = ins;
  89. }
  90.  
  91. public void insertBefore(E o, DLLNode<E> before) {
  92. if(before == first){
  93. insertFirst(o);
  94. return;
  95. }
  96. DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  97. before.pred.succ = ins;
  98. before.pred = ins;
  99. }
  100.  
  101. public E deleteFirst() {
  102. if (first != null) {
  103. DLLNode<E> tmp = first;
  104. first = first.succ;
  105. if (first != null) first.pred = null;
  106. if (first == null)
  107. last = null;
  108. return tmp.element;
  109. } else
  110. return null;
  111. }
  112.  
  113. public E deleteLast() {
  114. if (first != null) {
  115. if (first.succ == null)
  116. return deleteFirst();
  117. else {
  118. DLLNode<E> tmp = last;
  119. last = last.pred;
  120. last.succ = null;
  121. return tmp.element;
  122. }
  123. }
  124. // else throw Exception
  125. return null;
  126. }
  127.  
  128. public E delete(DLLNode<E> node) {
  129. if(node==first){
  130. deleteFirst();
  131. return node.element;
  132. }
  133. if(node==last){
  134. deleteLast();
  135. return node.element;
  136. }
  137. node.pred.succ = node.succ;
  138. node.succ.pred = node.pred;
  139. return node.element;
  140.  
  141. }
  142.  
  143. @Override
  144. public String toString() {
  145. String ret = new String();
  146. if (first != null) {
  147. DLLNode<E> tmp = first;
  148. ret += tmp + "<->";
  149. while (tmp.succ != null) {
  150. tmp = tmp.succ;
  151. ret += tmp + "<->";
  152. }
  153. } else
  154. ret = "Prazna lista!!!";
  155. return ret;
  156. }
  157.  
  158. public String toStringR() {
  159. String ret = new String();
  160. if (last != null) {
  161. DLLNode<E> tmp = last;
  162. ret += tmp + "<->";
  163. while (tmp.pred != null) {
  164. tmp = tmp.pred;
  165. ret += tmp + "<->";
  166. }
  167. } else
  168. ret = "Prazna lista!!!";
  169. return ret;
  170. }
  171.  
  172. public DLLNode<E> getFirst() {
  173. return first;
  174. }
  175.  
  176. public DLLNode<E> getLast() {
  177.  
  178. return last;
  179. }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement