Commit 2e93a1bc8207e7178f4a32f7946c6728c6e197bf

Authored by Adrian Chirita
1 parent 79723787

iterator, tester, litt kode forandret. MyLinkedList fjernet

Showing 1 changed file with 0 additions and 253 deletions   Show diff stats
src/oblig_2/MyLinkedList.java deleted
1   -package oblig_2;
2   -
3   -// 11th edition version
4   -public class MyLinkedList<E> implements MyList<E> {
5   - private Node<E> head, tail;
6   - private int size = 0; // Number of elements in the list
7   -
8   - /** Create an empty list */
9   - public MyLinkedList() {
10   - }
11   -
12   - /** Create a list from an array of objects */
13   - public MyLinkedList(E[] objects) {
14   - for (int i = 0; i < objects.length; i++)
15   - add(objects[i]);
16   - }
17   -
18   - /** Return the head element in the list */
19   - public E getFirst() {
20   - if (size == 0) {
21   - return null;
22   - }
23   - else {
24   - return head.element;
25   - }
26   - }
27   -
28   - /** Return the last element in the list */
29   - public E getLast() {
30   - if (size == 0) {
31   - return null;
32   - }
33   - else {
34   - return tail.element;
35   - }
36   - }
37   -
38   - /** Add an element to the beginning of the list */
39   - public void addFirst(E e) {
40   - Node<E> newNode = new Node<>(e); // Create a new node
41   - newNode.next = head; // link the new node with the head
42   - head = newNode; // head points to the new node
43   - size++; // Increase list size
44   -
45   - if (tail == null) // the new node is the only node in list
46   - tail = head;
47   - }
48   -
49   - /** Add an element to the end of the list */
50   - public void addLast(E e) {
51   - Node<E> newNode = new Node<>(e); // Create a new for element e
52   -
53   - if (tail == null) {
54   - head = tail = newNode; // The new node is the only node in list
55   - }
56   - else {
57   - tail.next = newNode; // Link the new with the last node
58   - tail = newNode; // tail now points to the last node
59   - }
60   -
61   - size++; // Increase size
62   - }
63   -
64   - @Override /** Add a new element at the specified index
65   - * in this list. The index of the head element is 0 */
66   - public void add(int index, E e) {
67   - if (index == 0) {
68   - addFirst(e);
69   - }
70   - else if (index >= size) {
71   - addLast(e);
72   - }
73   - else {
74   - Node<E> current = head;
75   - for (int i = 1; i < index; i++) {
76   - current = current.next;
77   - }
78   - Node<E> temp = current.next;
79   - current.next = new Node<>(e);
80   - (current.next).next = temp;
81   - size++;
82   - }
83   - }
84   -
85   - /** Remove the head node and
86   - * return the object that is contained in the removed node. */
87   - public E removeFirst() {
88   - if (size == 0) {
89   - return null;
90   - }
91   - else {
92   - E temp = head.element;
93   - head = head.next;
94   - size--;
95   - if (head == null) {
96   - tail = null;
97   - }
98   - return temp;
99   - }
100   - }
101   -
102   - /** Remove the last node and
103   - * return the object that is contained in the removed node. */
104   - public E removeLast() {
105   - if (size == 0) {
106   - return null;
107   - }
108   - else if (size == 1) {
109   - E temp = head.element;
110   - head = tail = null;
111   - size = 0;
112   - return temp;
113   - }
114   - else {
115   - Node<E> current = head;
116   -
117   - for (int i = 0; i < size - 2; i++) {
118   - current = current.next;
119   - }
120   -
121   - E temp = tail.element;
122   - tail = current;
123   - tail.next = null;
124   - size--;
125   - return temp;
126   - }
127   - }
128   -
129   - @Override /** Remove the element at the specified position in this
130   - * list. Return the element that was removed from the list. */
131   - public E remove(int index) {
132   - if (index < 0 || index >= size) {
133   - return null;
134   - }
135   - else if (index == 0) {
136   - return removeFirst();
137   - }
138   - else if (index == size - 1) {
139   - return removeLast();
140   - }
141   - else {
142   - Node<E> previous = head;
143   -
144   - for (int i = 1; i < index; i++) {
145   - previous = previous.next;
146   - }
147   -
148   - Node<E> current = previous.next;
149   - previous.next = current.next;
150   - size--;
151   - return current.element;
152   - }
153   - }
154   -
155   - @Override /** Override toString() to return elements in the list */
156   - public String toString() {
157   - StringBuilder result = new StringBuilder("[");
158   -
159   - Node<E> current = head;
160   - for (int i = 0; i < size; i++) {
161   - result.append(current.element);
162   - current = current.next;
163   - if (current != null) {
164   - result.append(", "); // Separate two elements with a comma
165   - }
166   - else {
167   - result.append("]"); // Insert the closing ] in the string
168   - }
169   - }
170   -
171   - return result.toString();
172   - }
173   -
174   - @Override /** Clear the list */
175   - public void clear() {
176   - size = 0;
177   - head = tail = null;
178   - }
179   -
180   - @Override /** Return true if this list contains the element e */
181   - public boolean contains(Object e) {
182   - // Left as an exercise
183   - return true;
184   - }
185   -
186   - @Override /** Return the element at the specified index */
187   - public E get(int index) {
188   - // Left as an exercise
189   - return null;
190   - }
191   -
192   - @Override /** Return the index of the head matching element in
193   - * this list. Return -1 if no match. */
194   - public int indexOf(Object e) {
195   - // Left as an exercise
196   - return 0;
197   - }
198   -
199   - @Override /** Return the index of the last matching element in
200   - * this list. Return -1 if no match. */
201   - public int lastIndexOf(E e) {
202   - // Left as an exercise
203   - return 0;
204   - }
205   -
206   - @Override /** Replace the element at the specified position
207   - * in this list with the specified element. */
208   - public E set(int index, E e) {
209   - // Left as an exercise
210   - return null;
211   - }
212   -
213   - @Override /** Override iterator() defined in Iterable */
214   - public java.util.Iterator<E> iterator() {
215   - return new LinkedListIterator();
216   - }
217   -
218   - private class LinkedListIterator
219   - implements java.util.Iterator<E> {
220   - private Node<E> current = head; // Current index
221   -
222   - @Override
223   - public boolean hasNext() {
224   - return (current != null);
225   - }
226   -
227   - @Override
228   - public E next() {
229   - E e = current.element;
230   - current = current.next;
231   - return e;
232   - }
233   -
234   - @Override
235   - public void remove() {
236   - // Left as an exercise
237   - }
238   - }
239   -
240   - private static class Node<E> {
241   - E element;
242   - Node<E> next;
243   - public Node(E element) {
244   - this.element = element;
245   - }
246   - }
247   -
248   - @Override /** Return the number of elements in this list */
249   - public int size() {
250   - return size;
251   - }
252   - }
253   -