Advertisement
xatzisktv

Untitled

Feb 12th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. class ListCell<T>
  2. { T data;
  3. ListCell<T> next;
  4. ListCell<T> previous;
  5.  
  6. public ListCell(T x, ListCell<T> c)
  7. { previous = null;
  8. data = x;
  9. next = c;
  10. }
  11. public ListCell(ListCell<T> previous, T x , ListCell<T> c){
  12. this.previous = previous;
  13. this.data = data;
  14. this.next = c;
  15. }
  16. }
  17.  
  18. class ListException extends RuntimeException
  19. { public ListException(String s)
  20. { super(s);
  21. }
  22. }
  23.  
  24. public class LList<T>
  25. { private ListCell<T> front;
  26.  
  27. public LList()
  28. { front = null;
  29. }
  30.  
  31. public void addToFront(T x)
  32. { front = new ListCell<T>(x, front);
  33. }
  34.  
  35. public void addToBack(T x)
  36. { if (front==null)
  37. front = new ListCell<T>(x, front);
  38. else
  39. { ListCell<T> c = front;
  40. while (c.next != null)
  41. c = c.next;
  42. c.next = new ListCell<T>(x, null);
  43. }
  44. }
  45.  
  46. public void removeLeft(){
  47. if(front==null){
  48. throw new ListException("List is empty");
  49. }
  50. else{
  51. front = front.next;
  52. front.previous = null;
  53.  
  54. }
  55. }
  56. public void removeRight(){
  57. if(front == null){
  58. throw new ListException("List is empty");
  59. }
  60. if(front.next == null){
  61. front = null;
  62. return;
  63. }
  64. ListCell<T> c = front;
  65. while(c.next.next != null){
  66. c = c.next;
  67. }
  68. c.next = null;
  69. }
  70.  
  71. public T elementAt(int n)
  72. { ListCell<T> c = front;
  73. for (int i = 0; i<n; i++)
  74. { if (c == null)
  75. throw new ListException("no element at position "+n);
  76. c = c.next;
  77. }
  78. if (c == null)
  79. throw new ListException("no element at position "+n);
  80. return c.data;
  81. }
  82.  
  83. public int length()
  84. { ListCell<T> c = front;
  85. int result = 0;
  86. while (c != null)
  87. { result++;
  88. c = c.next;
  89. }
  90. return result;
  91. }
  92.  
  93. // exercise 1
  94. public String toString()
  95. { StringBuffer sb = new StringBuffer("<");
  96. ListCell<T> c = front;
  97. while (c != null)
  98. { sb.append(c.data+",");
  99. c = c.next;
  100. }
  101. if (sb.length()>1)
  102. sb.setLength(sb.length()-1); // remove trailing comma
  103. return(sb+">");
  104. }
  105.  
  106. // exercise 2
  107. public int find(T x)
  108. { int pos = 0;
  109. ListCell<T> c = front;
  110. while (c!=null && !c.data.equals(x))
  111. { c = c.next;
  112. pos++;
  113. }
  114. if (c!=null)
  115. return pos;
  116. else
  117. return -1;
  118. }
  119.  
  120. // exercise 4
  121. public boolean removeAll(T x)
  122. { ListCell<T> c = front;
  123. ListCell<T> prev = null;
  124. boolean removedOne = false;
  125. while (c != null)
  126. { if (c.data.equals(x))
  127. { if (prev == null)
  128. front = c.next;
  129. else
  130. prev.next = c.next;
  131. removedOne = true;
  132. }
  133. else
  134. prev = c;
  135. c = c.next;
  136. }
  137. return removedOne;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement