Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. package linkedlist;
  2.  
  3. import java.util.Comparator;
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6. import java.util.Spliterator;
  7. import java.util.function.Consumer;
  8.  
  9. public class SimpleLinkedList<T extends Comparable<T>> implements ISimpleList<T>, IStack<T>, ISortable<T>, Iterable<T> {
  10.  
  11. private Element<T> tail = new Element<>(null, null);
  12. private Element<T> head = new Element<>(tail, null);
  13.  
  14. private int listCount = 0;
  15.  
  16. @Override
  17. public void add(T toAdd) {
  18.  
  19. Element lastInList = head.getLastValidElement();
  20. Element<T> newElement = new Element<T>(tail, toAdd);
  21.  
  22. if(!lastInList.equals(tail) || !lastInList.equals(head)) {
  23.  
  24. lastInList.setNext(newElement);
  25. } else {
  26.  
  27. head.setNext(newElement);
  28. }
  29.  
  30. listCount++;
  31. }
  32.  
  33. @Override
  34. public void add(int index, T toAdd) throws IndexOutOfBoundsException {
  35.  
  36. if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
  37.  
  38. // CurrentElement is our start and will get increased each loop iteration.
  39. Element currentElement = head;
  40.  
  41. int counter = 0;
  42. while(currentElement.hasNext()){
  43.  
  44. // Next Element is the ancessor of the current element
  45. // It replaces the current element at the end of the iteration.
  46. Element nextElement = currentElement.getNext();
  47.  
  48. // Checks if the element is valid, only head and tail doesnt have data sets.
  49. if(counter == index){
  50.  
  51. Element<T> newElement = new Element<T>(nextElement, toAdd);
  52. currentElement.setNext(newElement);
  53. listCount++;
  54. }
  55.  
  56. currentElement = nextElement;
  57. counter++;
  58. }
  59. }
  60.  
  61. @Override
  62. public void set(int index, T toSet) throws IndexOutOfBoundsException {
  63.  
  64. remove(index);
  65. add(index, toSet);
  66. }
  67.  
  68. @Override
  69. public void addAll(Iterable<T> collection) throws NullPointerException {
  70.  
  71. for(T t : collection)
  72. add(t);
  73.  
  74. }
  75.  
  76. @Override
  77. public T remove(T toSearch) {
  78.  
  79. int indexOf = indexOf(toSearch);
  80.  
  81. if(indexOf <= 0) return null;
  82.  
  83. return remove(indexOf);
  84. }
  85.  
  86. @Override
  87. public T remove(int index) throws IndexOutOfBoundsException {
  88.  
  89. if(index >= size() || index < 0) throw new IndexOutOfBoundsException();
  90.  
  91. // CurrentElement is our start and will get increased each loop iteration.
  92. Element currentElement = head;
  93.  
  94. int counter = 0;
  95. while(currentElement.hasNext()){
  96.  
  97. // Next Element is the ancessor of the current element
  98. // It replaces the current element at the end of the iteration.
  99. Element nextElement = currentElement.getNext();
  100.  
  101. // Checks if the element is valid, only head and tail doesnt have data sets.
  102. if(!nextElement.hasData()){ return null; }
  103. if(counter == index){
  104.  
  105. currentElement.setNext(nextElement.getNext());
  106. listCount--;
  107.  
  108. return (T)nextElement.getData();
  109. }
  110.  
  111. currentElement = nextElement;
  112. counter++;
  113. }
  114.  
  115. return null;
  116. }
  117.  
  118. @Override
  119. public void clear() {
  120. this.head.setNext(tail);
  121. listCount = 0;
  122. }
  123.  
  124. @Override
  125. public boolean contains(T toSearch) {
  126.  
  127. // CurrentElement is our start and will get increased each loop iteration.
  128. Element currentElement = head;
  129.  
  130. int counter = 0;
  131. while(currentElement.hasNext()){
  132.  
  133. // Next Element is the ancessor of the current element
  134. // It replaces the current element at the end of the iteration.
  135. Element nextElement = currentElement.getNext();
  136.  
  137. // Checks if the element is valid, only head and tail doesnt have data sets.
  138. if(!nextElement.hasData()){ return false; }
  139. if(nextElement.getData().equals(toSearch)){
  140.  
  141. return true;
  142. }
  143.  
  144. currentElement = nextElement;
  145. counter++;
  146. }
  147.  
  148. return false;
  149. }
  150.  
  151. @Override
  152. public int indexOf(T toFind) {
  153.  
  154. // CurrentElement is our start and will get increased each loop iteration.
  155. Element currentElement = head;
  156.  
  157. int counter = 0;
  158. while(currentElement.hasNext()){
  159.  
  160. // Next Element is the ancessor of the current element
  161. // It replaces the current element at the end of the iteration.
  162. Element nextElement = currentElement.getNext();
  163.  
  164. // Checks if the element is valid, only head and tail doesnt have data sets.
  165. if(!nextElement.hasData()){ return -1; }
  166. if(nextElement.getData().equals(toFind)){
  167.  
  168. return counter;
  169. }
  170.  
  171. currentElement = nextElement;
  172. counter++;
  173. }
  174.  
  175. return -1;
  176. }
  177.  
  178. @Override
  179. public T get(int index) throws IndexOutOfBoundsException, NoSuchElementException {
  180.  
  181. if(index == size() && index <= 0) throw new NoSuchElementException();
  182. if(index < 0 || index >= size()) throw new IndexOutOfBoundsException();
  183.  
  184. Element currentElement = head;
  185.  
  186. int counter = 0;
  187. while(currentElement.hasNext()){
  188.  
  189. Element nextElement = currentElement.getNext();
  190.  
  191. if(nextElement.equals(tail)){ return null; }
  192. if(counter == index){ return (T)nextElement.getData(); }
  193.  
  194. currentElement = nextElement;
  195. counter++;
  196. }
  197.  
  198. return null;
  199. }
  200.  
  201. @Override
  202. public T getFirst() {
  203.  
  204. if(size() <= 0) throw new NoSuchElementException();
  205.  
  206. return (T)head.getNext().getData();
  207. }
  208.  
  209. @Override
  210. public T getLast() {
  211.  
  212. if(size() <= 0) throw new NoSuchElementException();
  213.  
  214. return (T)head.getLastValidElement().getData();
  215. }
  216.  
  217.  
  218. @Override
  219. public ISimpleList<T> subList(int fromIndex, int toIndex) throws IndexOutOfBoundsException, IllegalArgumentException {
  220.  
  221. if(fromIndex > toIndex) throw new IllegalArgumentException();
  222.  
  223. ISimpleList<T> list = new SimpleLinkedList<>();
  224.  
  225. for(int index = fromIndex; index <= toIndex; index++) {
  226.  
  227. T entry = get(index);
  228. list.add(entry);
  229.  
  230. System.out.println(entry);
  231. }
  232.  
  233. return list;
  234. }
  235.  
  236. @Override
  237. public boolean isEmpty() {
  238. return size() <= 0;
  239. }
  240.  
  241. @Override
  242. public int size() {
  243. return this.listCount;
  244. }
  245.  
  246.  
  247. //////////////////////////////////
  248. // Stack & Sort methods
  249. /////////////////////////////////
  250.  
  251.  
  252. @Override
  253. public void push(T toAdd) {
  254.  
  255. add(size(), toAdd);
  256. }
  257.  
  258. @Override
  259. public T pop() throws NoSuchElementException {
  260.  
  261. if(size() <= 0) throw new NoSuchElementException();
  262.  
  263. return remove(size()-1);
  264. }
  265.  
  266. private Element<T> getElementAt(int index){
  267.  
  268. Element currentElement = head;
  269.  
  270. int counter = 0;
  271. while(currentElement.hasNext()){
  272.  
  273. Element nextElement = currentElement.getNext();
  274.  
  275. if(counter == index){ return nextElement; }
  276.  
  277. currentElement = nextElement;
  278. counter++;
  279. }
  280.  
  281. return null;
  282. }
  283.  
  284. public void switchElements(int indexOne, int indexTwo){
  285.  
  286. Element elementOne = getElementAt(indexOne);
  287. Element elementTwo = getElementAt(indexTwo);
  288.  
  289. Element elementBeforeOne = getElementAt(indexOne-1);
  290. Element elementBeforeTwo = getElementAt(indexTwo-1);
  291.  
  292. if(elementBeforeOne == null)
  293. elementBeforeOne = head;
  294.  
  295. if(elementBeforeTwo != null){
  296.  
  297. elementBeforeOne.setNext(elementTwo);
  298. elementBeforeTwo.setNext(elementOne);
  299.  
  300. Element elementAfterOne = elementOne.getNext();
  301. elementOne.setNext(elementTwo.getNext());
  302. elementTwo.setNext(elementAfterOne);
  303. }
  304. }
  305.  
  306. @Override
  307. public void sort() {
  308.  
  309. sort(new Comparator<T>() {
  310. @Override
  311. public int compare(T o1, T o2) {
  312.  
  313. return o1.compareTo(o2);
  314. }
  315. });
  316. }
  317.  
  318. @Override
  319. public void sort(Comparator<? super T> c) {
  320.  
  321. for(int index = 0; index < listCount; index++){
  322.  
  323. Element<T> currentElement = getElementAt(index);
  324.  
  325. for(int sindex = index; sindex < listCount; sindex++){
  326.  
  327. Element<T> innerElement = getElementAt(sindex);
  328.  
  329. if(currentElement.getData() == null || innerElement.getData() == null) continue;
  330.  
  331. if( c.compare(currentElement.getData(), innerElement.getData()) > 0){
  332.  
  333. switchElements(index, sindex);
  334. currentElement = getElementAt(index);
  335. }
  336. }
  337. }
  338. }
  339.  
  340.  
  341. //////////////////////////////////
  342. // Iterating methods
  343. /////////////////////////////////
  344.  
  345.  
  346. @Override
  347. public Iterator<T> iterator() {
  348.  
  349. T[] array = (T[])new Comparable[size()];
  350.  
  351. for (int index = 0; index < size(); index++)
  352. array[index] = get(index);
  353.  
  354. return new ListIterator<T>(array);
  355. }
  356.  
  357. @Override
  358. public void forEach(Consumer<? super T> action) {
  359.  
  360. }
  361.  
  362. @Override
  363. public Spliterator<T> spliterator() {
  364. return null;
  365. }
  366. }
  367.  
  368. package linkedlist;
  369.  
  370. public class Element<T> {
  371.  
  372. private T data;
  373. private Element next = null;
  374.  
  375. public Element(Element next, T data){
  376.  
  377. this.data = data;
  378. this.next = next;
  379. }
  380.  
  381. public T getData() {
  382. return data;
  383. }
  384. public boolean hasData(){ return getData() != null; }
  385. public void setData(T data) {
  386. this.data = data;
  387. }
  388.  
  389. public boolean hasNext(){ return getNext() != null; }
  390. public Element getNext() {
  391. return next;
  392. }
  393.  
  394. public void setNext(Element next) {
  395. this.next = next;
  396. }
  397.  
  398. public Element getLastValidElement(){
  399.  
  400. if(!next.hasNext()) {
  401. return this;
  402. }
  403. else return next.getLastValidElement();
  404. }
  405. }
  406.  
  407. package linkedlist;
  408.  
  409. import java.util.Iterator;
  410. import java.util.NoSuchElementException;
  411. import java.util.function.Consumer;
  412.  
  413. public class ListIterator<T> implements Iterator<T> {
  414.  
  415. private T[] array;
  416. private int counter = 0;
  417.  
  418. public ListIterator(T[] array){
  419.  
  420. this.array = array;
  421. }
  422.  
  423. @Override
  424. public boolean hasNext() {
  425. return counter < array.length;
  426. }
  427.  
  428. @Override
  429. public T next() {
  430.  
  431. if(!hasNext()) throw new NoSuchElementException();
  432.  
  433. T nextElement = array[counter];
  434. counter++;
  435.  
  436. return nextElement;
  437. }
  438.  
  439. @Override
  440. public void forEachRemaining(Consumer<? super T> action) {
  441.  
  442. if(action == null) return;
  443.  
  444. while (hasNext())
  445. action.accept(next());
  446. }
  447.  
  448. @Override
  449. public void remove() {}
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement