fix: correct minor syntax issues
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
public class MyLinkedList<T> implements MyList<T>
|
public class MyLinkedList<T> implements MyList<T>
|
||||||
{
|
{
|
||||||
private Node head;
|
private Node<T> head;
|
||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
public void add(T value) {
|
public void add(T value) {
|
||||||
if (head == null) {
|
if (head == null) {
|
||||||
this.head = new Node<>(value);
|
this.head = new Node<>(value);
|
||||||
} else {
|
} else {
|
||||||
Node temp = head;
|
Node<T> temp = head;
|
||||||
while (temp.getNext() != null) {
|
while (temp.getNext() != null) {
|
||||||
temp = temp.getNext();
|
temp = temp.getNext();
|
||||||
}
|
}
|
||||||
@@ -18,12 +18,12 @@ public class MyLinkedList<T> implements MyList<T>
|
|||||||
public T get(int index)
|
public T get(int index)
|
||||||
{
|
{
|
||||||
int currentIndex = 0;
|
int currentIndex = 0;
|
||||||
Node temp = head;
|
Node<T> temp = head;
|
||||||
while(temp != null)
|
while(temp != null)
|
||||||
{
|
{
|
||||||
if (currentIndex == index)
|
if (currentIndex == index)
|
||||||
{
|
{
|
||||||
return (T) temp.getValue();
|
return temp.getValue();
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -36,6 +36,9 @@ public class MyLinkedList<T> implements MyList<T>
|
|||||||
}
|
}
|
||||||
public void remove(int index)
|
public void remove(int index)
|
||||||
{
|
{
|
||||||
|
if (index < 0 || index >= size) {
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
if (index == 0)
|
if (index == 0)
|
||||||
{
|
{
|
||||||
head = head.getNext();
|
head = head.getNext();
|
||||||
@@ -44,7 +47,7 @@ public class MyLinkedList<T> implements MyList<T>
|
|||||||
|
|
||||||
}
|
}
|
||||||
int currentIndex = 0;
|
int currentIndex = 0;
|
||||||
Node temp = head;
|
Node<T> temp = head;
|
||||||
while (temp != null)
|
while (temp != null)
|
||||||
{
|
{
|
||||||
if (currentIndex == index-1)
|
if (currentIndex == index-1)
|
||||||
@@ -64,16 +67,15 @@ public class MyLinkedList<T> implements MyList<T>
|
|||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
Node temp = head;
|
Node<T> temp = head;
|
||||||
for (int i = 0; i < index; i++) {
|
for (int i = 0; i < index; i++) {
|
||||||
temp = temp.getNext();
|
temp = temp.getNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
T oldValue = (T) temp.getValue();
|
T oldValue = temp.getValue();
|
||||||
temp.setValue(value);
|
temp.setValue(value);
|
||||||
return oldValue;
|
return oldValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
@@ -81,10 +83,11 @@ public class MyLinkedList<T> implements MyList<T>
|
|||||||
{
|
{
|
||||||
return size == 0;
|
return size == 0;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
Object[] res = new Object[size];
|
Object[] res = new Object[size];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
Node temp = head;
|
Node<T> temp = head;
|
||||||
while (temp != null) {
|
while (temp != null) {
|
||||||
res[index] = temp.getValue();
|
res[index] = temp.getValue();
|
||||||
index++;
|
index++;
|
||||||
@@ -92,7 +95,8 @@ public class MyLinkedList<T> implements MyList<T>
|
|||||||
}
|
}
|
||||||
return Arrays.toString(res);
|
return Arrays.toString(res);
|
||||||
}
|
}
|
||||||
public static class Node<T> {
|
|
||||||
|
private static class Node<T> {
|
||||||
private T value;
|
private T value;
|
||||||
private Node<T> next;
|
private Node<T> next;
|
||||||
|
|
||||||
@@ -116,6 +120,4 @@ public class MyLinkedList<T> implements MyList<T>
|
|||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ public interface MyList<T> {
|
|||||||
|
|
||||||
T set(int index, T element);
|
T set(int index, T element);
|
||||||
|
|
||||||
T remove(int index);
|
void remove(int index);
|
||||||
|
|
||||||
int size();
|
int size();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user