Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. class SLLNode<E extends Comparable<E>> {
  2. protected E element;
  3. protected SLLNode<E> succ;
  4.  
  5. public SLLNode(E elem, SLLNode<E> succ) {
  6. this.element = elem;
  7. this.succ = succ;
  8. }
  9.  
  10. @Override
  11. public String toString() {
  12. return element.toString();
  13. }
  14. }
  15.  
  16. class SLL<E extends Comparable<E>> {
  17. private SLLNode<E> first;
  18.  
  19. public SLL() {
  20. // Construct an empty SLL
  21. this.first = null;
  22. }
  23.  
  24. public void deleteList() {
  25. first = null;
  26. }
  27.  
  28. public int length() {
  29. int ret;
  30. if (first != null) {
  31. SLLNode<E> tmp = first;
  32. ret = 1;
  33. while (tmp.succ != null) {
  34. tmp = tmp.succ;
  35. ret++;
  36. }
  37. return ret;
  38. } else
  39. return 0;
  40.  
  41. }
  42.  
  43. @Override
  44. public String toString() {
  45. String ret = new String();
  46. if (first != null) {
  47. SLLNode<E> tmp = first;
  48. ret += tmp + "->";
  49. while (tmp.succ != null) {
  50. tmp = tmp.succ;
  51. ret += tmp + "->";
  52. }
  53. } else
  54. ret = "Prazna lista!!!";
  55. return ret;
  56. }
  57.  
  58. public void insertFirst(E o) {
  59. SLLNode<E> ins = new SLLNode<E>(o, first);
  60. first = ins;
  61. }
  62.  
  63. public void insertAfter(E o, SLLNode<E> node) {
  64. if (node != null) {
  65. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  66. node.succ = ins;
  67. } else {
  68. System.out.println("Dadenot jazol e null");
  69. }
  70. }
  71.  
  72. public void insertBefore(E o, SLLNode<E> before) {
  73.  
  74. if (first != null) {
  75. SLLNode<E> tmp = first;
  76. if (first == before) {
  77. this.insertFirst(o);
  78. return;
  79. }
  80. // ako first!=before
  81. while (tmp.succ != before)
  82. tmp = tmp.succ;
  83. if (tmp.succ == before) {
  84. SLLNode<E> ins = new SLLNode<E>(o, before);
  85. tmp.succ = ins;
  86. } else {
  87. System.out.println("Elementot ne postoi vo listata");
  88. }
  89. } else {
  90. System.out.println("Listata e prazna");
  91. }
  92. }
  93.  
  94. public void insertLast(E o) {
  95. if (first != null) {
  96. SLLNode<E> tmp = first;
  97. while (tmp.succ != null)
  98. tmp = tmp.succ;
  99. SLLNode<E> ins = new SLLNode<E>(o, null);
  100. tmp.succ = ins;
  101. } else {
  102. insertFirst(o);
  103. }
  104. }
  105.  
  106. public SLLNode<E> getFirst() {
  107. return first;
  108. }
  109.  
  110. public Iterator<E> iterator() {
  111. // Return an iterator that visits all elements of this list, in
  112. // left-to-right order.
  113. return new LRIterator<E>();
  114. }
  115.  
  116. // //////////Inner class ////////////
  117.  
  118. private class LRIterator<E extends Comparable<E>> implements Iterator<E> {
  119.  
  120. private SLLNode<E> place;
  121.  
  122. @SuppressWarnings("unchecked")
  123. private LRIterator() {
  124. place = (SLLNode<E>) first;
  125. }
  126.  
  127. public boolean hasNext() {
  128. return (place != null);
  129. }
  130.  
  131. public E next() {
  132. if (place == null)
  133. throw new NoSuchElementException();
  134. E nextElem = place.element;
  135. place = place.succ;
  136. return nextElem;
  137. }
  138.  
  139. public void remove() {
  140. // Not implemented
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement