SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.util.Iterator; | |
| 2 | ||
| 3 | public class SimpleLinkedList<ELEMENT extends Comparable<ELEMENT>> implements ILinkedList<ELEMENT> {
| |
| 4 | ||
| 5 | //region Node Class | |
| 6 | ||
| 7 | private class Node<ELEMENT> {
| |
| 8 | public ELEMENT item; | |
| 9 | public Node<ELEMENT> next; | |
| 10 | ||
| 11 | public Node() {
| |
| 12 | this(null, null); | |
| 13 | } | |
| 14 | public Node(ELEMENT item) {
| |
| 15 | this(item, null); | |
| 16 | } | |
| 17 | public Node(ELEMENT item, Node<ELEMENT> next) {
| |
| 18 | this.item = item; | |
| 19 | this.next = next; | |
| 20 | } | |
| 21 | ||
| 22 | @Override | |
| 23 | public String toString() {
| |
| 24 | return this.item.toString(); | |
| 25 | } | |
| 26 | } | |
| 27 | //endregion | |
| 28 | ||
| 29 | //region Attributes | |
| 30 | ||
| 31 | protected Node<ELEMENT> head; | |
| 32 | protected int count; | |
| 33 | protected Node<ELEMENT> tail; | |
| 34 | //endregion | |
| 35 | ||
| 36 | //region Constructors | |
| 37 | ||
| 38 | public SimpleLinkedList() {
| |
| 39 | this.head = null; | |
| 40 | this.count = 0; | |
| 41 | this.tail = null; | |
| 42 | } | |
| 43 | //endregion | |
| 44 | ||
| 45 | //region Linked List Methods | |
| 46 | ||
| 47 | // Returns the number of elements in this list. | |
| 48 | public int size() {
| |
| 49 | return this.count; | |
| 50 | } | |
| 51 | ||
| 52 | public void addFirstRookieVersion(ELEMENT item) {
| |
| 53 | if (this.count == 0) {
| |
| 54 | this.head = this.tail = new Node<ELEMENT>(item, null); | |
| 55 | ++this.count; | |
| 56 | } else {
| |
| 57 | Node<ELEMENT> temp = new Node<ELEMENT>(item, null); | |
| 58 | temp.next = this.head; | |
| 59 | this.head = temp; | |
| 60 | ++this.count; | |
| 61 | } | |
| 62 | } | |
| 63 | // Inserts the specified element at the beginning of this list. | |
| 64 | public void addFirst(ELEMENT item) {
| |
| 65 | Node<ELEMENT> temp = new Node<ELEMENT>(item, this.head); | |
| 66 | if (this.count == 0) {
| |
| 67 | this.tail = temp; | |
| 68 | } | |
| 69 | this.head = temp; | |
| 70 | ++this.count; | |
| 71 | } | |
| 72 | ||
| 73 | public void addLastRookieVersion(ELEMENT item) {
| |
| 74 | if (this.count == 0) {
| |
| 75 | this.head = this.tail = new Node<ELEMENT>(item, null); | |
| 76 | ++this.count; | |
| 77 | } else {
| |
| 78 | Node<ELEMENT> temp = new Node<ELEMENT>(item, null); | |
| 79 | this.tail.next = temp; | |
| 80 | this.tail = temp; | |
| 81 | ++this.count; | |
| 82 | } | |
| 83 | } | |
| 84 | // Appends the specified element to the end of this list. | |
| 85 | public void addLast(ELEMENT item) {
| |
| 86 | Node<ELEMENT> temp = new Node<ELEMENT>(item, null); | |
| 87 | if (this.count == 0) {
| |
| 88 | this.head = temp; | |
| 89 | } else {
| |
| 90 | this.tail.next = temp; | |
| 91 | } | |
| 92 | this.tail = temp; | |
| 93 | ++this.count; | |
| 94 | } | |
| 95 | ||
| 96 | // Removes and returns the first element from this list. | |
| 97 | public ELEMENT removeFirst() {
| |
| 98 | if (this.count == 0) {
| |
| 99 | throw new RuntimeException("La lista está vacía...");
| |
| 100 | } | |
| 101 | ELEMENT item = this.head.item; | |
| 102 | this.head = this.head.next; | |
| 103 | if (this.head == null) {
| |
| 104 | this.tail = null; | |
| 105 | } | |
| 106 | --this.count; | |
| 107 | return item; | |
| 108 | } | |
| 109 | ||
| 110 | // Removes and returns the last element from this list. | |
| 111 | public ELEMENT removeLast() {
| |
| 112 | if (this.count == 0) {
| |
| 113 | throw new RuntimeException("La lista está vacía...");
| |
| 114 | } | |
| 115 | ELEMENT item = this.tail.item; | |
| 116 | if (this.head.next == null) {
| |
| 117 | this.head = this.tail = null; | |
| 118 | } else {
| |
| 119 | Node<ELEMENT> skip = this.head; | |
| 120 | while (skip.next.next != null) {
| |
| 121 | skip = skip.next; | |
| 122 | } | |
| 123 | this.tail = skip; | |
| 124 | this.tail.next = null; | |
| 125 | } | |
| 126 | --this.count; | |
| 127 | return item; | |
| 128 | } | |
| 129 | //endregion | |
| 130 | ||
| 131 | public void Mostrar() {
| |
| 132 | for (Node skip = this.head; skip!= null; skip = skip.next) {
| |
| 133 | System.out.printf("%s ", skip.toString() );
| |
| 134 | } | |
| 135 | } | |
| 136 | ||
| 137 | public void addInOrder(ELEMENT item) {
| |
| 138 | if (this.size() == 0) {
| |
| 139 | this.head = this.tail = new Node<ELEMENT>(item, null); | |
| 140 | ++this.count; | |
| 141 | } | |
| 142 | else {
| |
| 143 | if (item.compareTo(this.head.item) <= 0) {
| |
| 144 | this.addFirst(item); | |
| 145 | } | |
| 146 | else {
| |
| 147 | if (item.compareTo(this.tail.item) > 0) {
| |
| 148 | this.addLast(item); | |
| 149 | } | |
| 150 | else {
| |
| 151 | Node<ELEMENT> skip = this.head; | |
| 152 | while ((skip != null) && (skip.next != null) && (item.compareTo(skip.next.item) > 0)) {
| |
| 153 | skip = skip.next; | |
| 154 | } | |
| 155 | if (skip == null) {
| |
| 156 | throw new RuntimeException("Algo está mal en el orden de los elementos de la lista...");
| |
| 157 | } | |
| 158 | else {
| |
| 159 | Node<ELEMENT> temp = new Node<ELEMENT>(item, skip.next); | |
| 160 | skip.next = temp; | |
| 161 | ++this.count; | |
| 162 | } | |
| 163 | } | |
| 164 | } | |
| 165 | } | |
| 166 | ||
| 167 | } | |
| 168 | ||
| 169 | ||
| 170 | ||
| 171 | ||
| 172 | //region Object Methods | |
| 173 | ||
| 174 | @Override | |
| 175 | public String toString() {
| |
| 176 | ||
| 177 | if (this.size() <=0) {
| |
| 178 | return ""; | |
| 179 | } | |
| 180 | ||
| 181 | // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html | |
| 182 | StringBuilder sb = new StringBuilder(); | |
| 183 | ||
| 184 | sb.append("[" + this.head.item.toString());
| |
| 185 | for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
| |
| 186 | sb.append(", " + skip.item.toString());
| |
| 187 | } | |
| 188 | sb.append("]");
| |
| 189 | ||
| 190 | return sb.toString(); | |
| 191 | } | |
| 192 | //endregion | |
| 193 | ||
| 194 | ||
| 195 | //region Iterable Methods | |
| 196 | @Override | |
| 197 | public Iterator<ELEMENT> iterator() {
| |
| 198 | return new SimpleLinkedListIterator(this.head); | |
| 199 | } | |
| 200 | ||
| 201 | private class SimpleLinkedListIterator implements Iterator<ELEMENT> {
| |
| 202 | private Node<ELEMENT> current; | |
| 203 | ||
| 204 | public SimpleLinkedListIterator(Node<ELEMENT> current) {
| |
| 205 | this.current = current; | |
| 206 | } | |
| 207 | ||
| 208 | @Override | |
| 209 | public boolean hasNext() {
| |
| 210 | return this.current != null; | |
| 211 | } | |
| 212 | ||
| 213 | @Override | |
| 214 | public ELEMENT next() {
| |
| 215 | if (!this.hasNext()) {
| |
| 216 | throw new RuntimeException("La lista está vacía...");
| |
| 217 | } | |
| 218 | ELEMENT item = this.current.item; | |
| 219 | this.current = this.current.next; | |
| 220 | return item; | |
| 221 | } | |
| 222 | ||
| 223 | } | |
| 224 | ||
| 225 | ||
| 226 | //endregion | |
| 227 | ||
| 228 | } |