I created my own implementation of a Singly Linked List. Is there anything I can improve on, in terms of effciency. Also, what other methods would you recommend for me to try and implement. My goal is to just understand the components of basic data structures.
LinkedList:
public class StratchLinkedList {
private Node head;
private int size;
public StratchLinkedList() {
head = null;
size = 0;
}
public void add(Object data) {
Node temp = new Node(data);
Node curr = head;
if (head == null) {
head = temp;
} else {
while (curr.getNext() != null) {
curr = curr.getNext();
}
curr.setNext(temp);
}
}
public void add(Object data, int index) {
Node temp = new Node(data);
Node curr = head;
if (index == 0){
temp.setNext(head);
this.head = temp;
} else{
for(int i = 1; i < index; i++){
curr = curr.getNext();
}
temp.setNext(curr.getNext());
curr.setNext(temp);
}
this.size++;
}
public void replace(Object data, int index) {
Node curr = head;
for (int i = 0; i < 0; i++){
curr = curr.getNext();
}
curr.setData(data);
}
public Object get(int index) {
Node curr = head;
for (int i = 0; i < index; i++){
curr = curr.getNext();
}
return curr.getData();
}
public void remove(int index) {
Node curr = head;
if (index == 0) {
head = head.getNext();
} else {
for (int i = 1; i < index; i++){
curr = curr.getNext();
}
curr.setNext(curr.getNext().getNext());
}
this.size--;
}
public int size() {
return this.size;
}
public String toString() {
String list = "";
list += "[" + this.head.getData() + "]";
Node curr = head.getNext();
while (curr != null){
list += "[" + curr.getData() + "]";
curr = curr.getNext();
}
return list;
}
}
Node:
public class Node {
Node next;
Object data;
public Node(Object data) {
this(data, null);
}
public Node(Object data, Node next) {
this.next = next;
this.data = data;
}
public Object getData() {
return this.data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return this.next;
}
public void setNext(Node nextNode) {
this.next = nextNode;
}
}
EDIT: Code working in progress.
public class StratchLinkedList<T> {
private Node head;
private Node tail;
private int size;
public StratchLinkedList() {
head = null;
tail = null;
size = 0;
}
public void insert(T data, int index) {
if (index > size) {
throw new IllegalArgumentException("The index [" + index
+ "] is greater than the currentent size [" + size + "].");
} else {
Node temp = new Node(data);
Node current = getNode(index);
if (index == 0) {
temp.setNext(head);
head = temp;
tail = head;
} else {
temp.setNext(current.getNext());
current.setNext(temp);
}
if ( index == size - 1 ) {
tail.setNext(temp);
tail = temp;
}
}
size++;
}
public void append(T data) {
insert(data, size);
}
public void replace(T data, int index) {
getNode(index).setData(data);
}
public void remove(int index) {
if (index == 0) {
head = head.getNext();
} else {
getNode(index).setNext(getNode(index).getNext().getNext());
}
this.size--;
}
private Node getNode(int index) {
if ( index > size ) {
throw new IllegalArgumentException("The index [" + index + "] is greater than the current size [" + size + "].");
}
Node current = head;
for (int i = 1; i < index; i++) {
current = current.getNext();
}
return current;
}
public T get(int index) {
return getNode(index).getData();
}
public int size() {
return this.size;
}
public String toString() {
StringBuilder builder = new StringBuilder();
Node current = head;
while( current != null ) {
builder.append("[" + current.getData() + "]");
current = current.getNext();
}
return builder.toString();
}
private class Node {
Node next;
T data;
public Node(T data) {
this(data, null);
}
public Node(T data, Node next) {
this.next = next;
this.data = data;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public Node getNext() {
return this.next;
}
public void setNext(Node nextNode) {
this.next = nextNode;
}
}
}