public class LinkedList { private Node head; private int listCount; public LinkedList() { head = new Node(null); listCount = 0; } public void add(Object data) { Node temp = new Node(data); Node current = head; while(current.getNext() != null) { current = current.getNext(); } current.setNext(temp); listCount++; } public boolean add(Object data, int index) { if (index<0 || index > listCount) return false; Node temp = new Node(data); Node current = head; for(int i = 1; i < index && current.getNext() != null; i++) { current = current.getNext(); } temp.setNext(current.getNext()); current.setNext(temp); listCount++;// increment the number of elements variable return true; } public Object get(int index) { // index must be 1 or higher if(index <= 0 || index > listCount) return null; Node current = head.getNext(); for(int i = 1; i < index; i++) { if(current.getNext() == null) return null; current = current.getNext(); } return current.getData(); } public boolean remove(int index) { // if the index is out of range, exit if(index < 1 || index > size()) return false; Node current = head; for(int i = 1; i < index; i++) { if(current.getNext() == null) return false; current = current.getNext(); } current.setNext(current.getNext().getNext()); listCount--; // decrement the number of elements variable return true; } public boolean remove(Object data) { Node current = head; while (current.getNext()!=null) { if (((current.getNext()).getData()).equals(data)) { current.setNext(current.getNext().getNext()); listCount--; // decrement the number of elements variable return true; } current = current.getNext(); } return false; } public int size() { return listCount; } public String toString() { Node current = head.getNext(); String output = ""; while(current != null) { output += "[" + current.getData().toString() + "]"; current = current.getNext(); } return output; } }