Guest User

Untitled

a guest
Oct 23rd, 2019
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. package aiz.list;
  3.  
  4. public class ListOne<T> implements IList<T>{
  5.  
  6. ElemOne<T> first;
  7. ElemOne<T> last;
  8. int count = 0;
  9.  
  10. @Override
  11. public void addFirst(T newData) {
  12. ElemOne<T> f = new ElemOne(newData);
  13. f.setNext(first);
  14. count++;
  15. first = f;
  16. }
  17.  
  18. @Override
  19. public void addLast(T newData) {
  20. ElemOne<T> f;
  21. count++;
  22. f = new ElemOne(newData);
  23.  
  24. if (first == null)
  25. {
  26. first = new ElemOne(newData);
  27. return;
  28. }
  29.  
  30. f.setNext(null);
  31. last = first;
  32.  
  33. while (last.getNext() != null)
  34. {
  35. last = last.getNext();
  36. }
  37. last.setNext(f);
  38. }
  39.  
  40. @Override
  41. public void addAtPosition(T newData, int position) throws ListException {
  42. if (position > size())
  43. {
  44. throw new ListException("Lista jest za mala");
  45. } else
  46. {
  47. count++;
  48. ElemOne<T> l = first;
  49. ElemOne<T> prev = first;
  50. ElemOne<T> nw;
  51. nw = new ElemOne(newData);
  52.  
  53. if (position == 0)
  54. {
  55. l = new ElemOne(newData);
  56. l.setNext(first);
  57. first = l;
  58. } else
  59. {
  60. for(int i = 0; i < position; i++)
  61. {
  62. prev = prev.getNext();
  63. }
  64. for(int i = 0; i < position; i++)
  65. {
  66. l = l.getNext();
  67. }
  68. nw.setNext(l);
  69. prev.setNext(nw);
  70. }
  71. }
  72. }
  73.  
  74. @Override
  75. public int size() {
  76. return count;
  77. }
  78.  
  79. @Override
  80. public T get(int position) throws ListException {
  81. ElemOne<T> f = first;
  82. for (int i = 1; i < position; i++) {
  83. f = f.getNext();
  84. }
  85. return f.getData();
  86. }
  87.  
  88. @Override
  89. public T removeFirst() throws ListException {
  90.  
  91. if (first == null) {
  92. first = first.getNext();
  93. count--;
  94. }
  95. return first.getData();
  96. }
  97.  
  98. @Override
  99. public T removeLast() throws ListException {
  100.  
  101. count--;
  102. for (last = this.first; last.getNext().getNext() != null; last = last.getNext())
  103. {
  104. last.setNext(null);
  105. }
  106. return first.getData();}
  107.  
  108. @Override
  109. public T remove(int position) throws ListException {
  110. if (position > size())
  111. {
  112. throw new ListException("Lista jest za mala");
  113. } else
  114. {
  115.  
  116. ElemOne<T> l = first;
  117. ElemOne<T> prev = first;
  118. if (position == 0)
  119. {
  120. first = first.getNext();
  121. } else
  122. {
  123. for (int i = 1; i < position - 1; i++)
  124. {
  125. prev = prev.getNext();
  126. }
  127. for (int i = 1; i < position; i++)
  128. {
  129. l = l.getNext();
  130. }
  131. prev.setNext(l.getNext());
  132. l = l.getNext();
  133. }
  134. return l.getData();
  135. }
  136. }
  137.  
  138. @Override
  139. public int find(T dataToFind) {
  140. int licz = 0;
  141. ElemOne<T> f = first;
  142. try {
  143. while (f != null && f.getData() != dataToFind)
  144. {
  145. f = f.getNext();
  146. licz++;
  147. }
  148.  
  149. if (f.getData() == dataToFind)
  150. {
  151. return licz;
  152. }
  153. } catch (NullPointerException e)
  154. {
  155. System.out.println("Nie ma takiego elementu");
  156. }
  157. return -1;
  158. }
  159.  
  160. @Override
  161. public boolean contains(T data)
  162. {
  163. ElemOne<T> f = first;
  164. if (f.getData() == data)
  165. {
  166. return true;
  167. }
  168. try {
  169. while (f != null && f.getData() != data) {
  170. f = f.getNext();
  171. if (f.getData() == data) {
  172. return true;
  173. }
  174.  
  175. }
  176. } catch (NullPointerException e) {
  177. System.out.println("nie ma takiego numeru");
  178. }
  179.  
  180. return false;
  181. }
  182.  
  183. @Override
  184. public void print() {
  185. ElemOne<T> f = first;
  186.  
  187. while (f != null)
  188. {
  189. System.out.print(f.getData() + " ");
  190. f = f.getNext();
  191. }
  192. }
  193.  
  194. }
RAW Paste Data