Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. 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.  
  18. class DLL<E> {
  19. private DLLNode<E> first, last;
  20.  
  21. public DLL() {
  22. // Construct an empty SLL
  23. this.first = null;
  24. this.last = null;
  25. }
  26.  
  27. public void deleteList() {
  28. first = null;
  29. last = null;
  30. }
  31.  
  32. public int length() {
  33. int ret;
  34. if (first != null) {
  35. DLLNode<E> tmp = first;
  36. ret = 1;
  37. while (tmp.succ != null) {
  38. tmp = tmp.succ;
  39. ret++;
  40. }
  41. return ret;
  42. } else
  43. return 0;
  44.  
  45. }
  46.  
  47. public void insertFirst(E o) {
  48. DLLNode<E> ins = new DLLNode<E>(o, null, first);
  49. if (first == null)
  50. last = ins;
  51. else
  52. first.pred = ins;
  53. first = ins;
  54. }
  55.  
  56. public void insertLast(E o) {
  57. if (first == null)
  58. insertFirst(o);
  59. else {
  60. DLLNode<E> ins = new DLLNode<E>(o, last, null);
  61. last.succ = ins;
  62. last = ins;
  63. }
  64. }
  65.  
  66. public void insertAfter(E o, DLLNode<E> after) {
  67. if(after==last){
  68. insertLast(o);
  69. return;
  70. }
  71. DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  72. after.succ.pred = ins;
  73. after.succ = ins;
  74. }
  75.  
  76. public void insertBefore(E o, DLLNode<E> before) {
  77. if(before == first){
  78. insertFirst(o);
  79. return;
  80. }
  81. DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  82. before.pred.succ = ins;
  83. before.pred = ins;
  84. }
  85.  
  86. public E deleteFirst() {
  87. if (first != null) {
  88. DLLNode<E> tmp = first;
  89. first = first.succ;
  90. first.pred = null;
  91. if (first == null)
  92. last = null;
  93. return tmp.element;
  94. } else
  95. return null;
  96. }
  97.  
  98. public E deleteLast() {
  99. if (first != null) {
  100. if (first.succ == null)
  101. return deleteFirst();
  102. else {
  103. DLLNode<E> tmp = last;
  104. last = last.pred;
  105. last.succ = null;
  106. return tmp.element;
  107. }
  108. }
  109. // else throw Exception
  110. return null;
  111. }
  112.  
  113.  
  114. @Override
  115. public String toString() {
  116. String ret = new String();
  117. if (first != null) {
  118. DLLNode<E> tmp = first;
  119. ret += tmp + "<->";
  120. while (tmp.succ != null) {
  121. tmp = tmp.succ;
  122. ret += tmp + "<->";
  123. }
  124. } else
  125. ret = "Prazna lista!!!";
  126. return ret;
  127. }
  128.  
  129. public DLLNode<E> getFirst() {
  130. return first;
  131. }
  132.  
  133. public DLLNode<E> getLast() {
  134.  
  135. return last;
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement