Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //Created by Julio Tentor <[email protected]>
- //
- import java.util.Iterator;
- public class SimpleLinkedList<ELEMENT extends Comparable <ELEMENT>> implements ILinkedList<ELEMENT> {
- //region Node Class
- @SuppressWarnings("hiding")
- private class Node<ELEMENT> {
- public ELEMENT item;
- public Node<ELEMENT> next;
- @SuppressWarnings("unused")
- public Node() {
- this(null, null);
- }
- @SuppressWarnings("unused")
- public Node(ELEMENT item) {
- this(item, null);
- }
- public Node(ELEMENT item, Node<ELEMENT> next) {
- this.item = item;
- this.next = next;
- }
- @Override
- public String toString() {
- return this.item.toString();
- }
- }
- //endregion
- //region Attributes
- protected Node<ELEMENT> head;
- protected int count;
- protected Node<ELEMENT> tail;
- //endregion
- //region Constructors
- public SimpleLinkedList() {
- this.head = null;
- this.count = 0;
- this.tail = null;
- }
- //endregion
- //region Linked List Methods
- // Returns the number of elements in this list.
- public int size() {
- return this.count;
- }
- public void addFirstRookieVersion(ELEMENT item) {
- if (this.count == 0) {
- this.head = this.tail = new Node<ELEMENT>(item, null);
- ++this.count;
- } else {
- Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
- temp.next = this.head;
- this.head = temp;
- ++this.count;
- }
- }
- // Inserts the specified element at the beginning of this list.
- public void addFirst(ELEMENT item) {
- Node<ELEMENT> temp = new Node<ELEMENT>(item, this.head);
- if (this.count == 0) {
- this.tail = temp;
- }
- this.head = temp;
- ++this.count;
- }
- public void addLastRookieVersion(ELEMENT item) {
- if (this.count == 0) {
- this.head = this.tail = new Node<ELEMENT>(item, null);
- ++this.count;
- } else {
- Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
- this.tail.next = temp;
- this.tail = temp;
- ++this.count;
- }
- }
- // Appends the specified element to the end of this list.
- public void addLast(ELEMENT e) {
- Node<ELEMENT> temp = new Node<ELEMENT>(e, null);
- if (this.count == 0) {
- this.head = temp;
- } else {
- this.tail.next = temp;
- }
- this.tail = temp;
- ++this.count;
- }
- // Removes and returns the first element from this list.
- public ELEMENT removeFirst() {
- if (this.count == 0) {
- throw new RuntimeException("La lista está vacía...");
- }
- ELEMENT item = this.head.item;
- this.head = this.head.next;
- if (this.head == null) {
- this.tail = null;
- }
- --this.count;
- return item;
- }
- // Removes and returns the last element from this list.
- public ELEMENT removeLast() {
- if (this.count == 0) {
- throw new RuntimeException("La lista está vacía...");
- }
- ELEMENT item = this.tail.item;
- if (this.head.next == null) {
- this.head = this.tail = null;
- } else {
- Node<ELEMENT> skip = this.head;
- while (skip.next.next != null) {
- skip = skip.next;
- }
- this.tail = skip;
- this.tail.next = null;
- }
- --this.count;
- return item;
- }
- public ELEMENT element() {
- if(this.size()<=0) {
- System.out.println("La cola está vacía");
- }
- return (ELEMENT) this.head.item;
- }
- @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
- public void addInOrder(ELEMENT item) {
- if(this.count==0) {
- this.head = this.tail = new Node(item,null);
- ++this.count;
- }else {
- if(item.compareTo(this.head.item)<=0) {
- this.addFirst(item);
- }else {
- if (item.compareTo(this.tail.item)>0) {
- this.addLast(item);
- }else {
- Node skip = this.head;
- for(;(skip!=null)&(item.compareTo((ELEMENT) skip.item)>0);skip = skip.next) {}
- if (skip == null) {
- throw new RuntimeException("Algo está mal respecto del orden de los elementos");
- }else {
- Node temp = new Node(item,skip.next);
- skip.next = temp;
- ++this.count;
- }
- }
- }
- }
- }
- @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
- public void addInOrderForVideoTitulo(Video video) {
- if(this.count==0) {
- this.head = this.tail = new Node(video,null);
- ++this.count;
- }else {
- if(video.compareTitulo(this.head.item)<=0) {
- this.addFirst((ELEMENT) video);
- }else {
- if (video.compareTitulo(this.tail.item)>0) {
- this.addLast((ELEMENT) video);
- }else {
- Node skip = this.head;
- for(;(skip!=null)&(video.compareTitulo((ELEMENT) skip.item)>0);skip = skip.next) {}
- if (skip == null) {
- throw new RuntimeException("Algo está mal respecto del orden de los elementos");
- }else {
- Node temp = new Node(video,skip.next);
- skip.next = temp;
- ++this.count;
- }
- }
- }
- }
- }
- //endregion
- //region Object Methods
- @Override
- public String toString() {
- if (this.size() <=0) {
- return "";
- }
- // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
- StringBuilder sb = new StringBuilder();
- sb.append("[" + this.head.item.toString());
- for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
- sb.append(", \n" + skip.item.toString());
- }
- sb.append("]");
- return sb.toString();
- }
- public Object[] toArray() {
- Object[] result = new Object[this.size()];
- int i = 0;
- for(Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next, i++) {
- result[i] = skip.item.toString();
- }
- return result;
- }
- public boolean search(Object object) {
- if (this.size()<=0)return false;
- for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
- if (this.head.item.equals(object)) {
- return true;
- }
- }
- return false;
- }
- //endregion
- //region Iterable Methods
- @Override
- public Iterator<ELEMENT> iterator() {
- return new SimpleLinkedListIterator(this.head);
- }
- private class SimpleLinkedListIterator implements Iterator<ELEMENT> {
- private Node<ELEMENT> current;
- public SimpleLinkedListIterator(Node<ELEMENT> current) {
- this.current = current;
- }
- @Override
- public boolean hasNext() {
- return this.current != null;
- }
- @Override
- public ELEMENT next() {
- if (!this.hasNext()) {
- throw new RuntimeException("La lista está vacía...");
- }
- ELEMENT item = this.current.item;
- this.current = this.current.next;
- return item;
- }
- }
- //endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement