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