Can anyone review the methods? If there are better ways to write a certain method I would appreciate it if anyone could tell me.
public class Node<E> {
E data;
Node<E> next;
public Node(E item) {
setData(item);
setNext(null);
}
public Node(E item, Node<E> Ref) {
setData(item);
setNext(Ref);
}
public E getData() {
return data;
}
public void setData(E item) {
data = item;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> Ref) {
next = Ref;
}
}
class MySinglyLinkedList<E> {
// attributes
private int size;
private Node<E> head;
// methods
public MySinglyLinkedList() {
size = 0;
head = null;
}
// Method size
private int sizeHelper(Node<E> head) { // Helper Method
if (head == null)
return 0;
else
return 1 + sizeHelper(head.next);
}
public int size() { // Wrapper Method
return sizeHelper(head);
}
// Method add
private boolean addHelper(Node<E> head, E data) { // Helper Method
if (head.next == null) {
head.next = new Node<E>(data);
return true;
} else
return addHelper(head.next, data);
}
public boolean add(E data) { // Wrapper Method
if (head == null) {
head = new Node<E>(data);
return true;
} else
return addHelper(head, data);
}
// Method toString
private String toStringHelper(Node<E> head) { // Helper Method
if (head == null)
return "";
else
return head.data + "\n" + toStringHelper(head.next);
}
public String toString() { // Wrapper Method
return toStringHelper(head);
}
// Method get
private Node<E> getNodeHelper(int i, Node<E> head) { // Helper Method
if (i == 0)
return head;
else
return getNodeHelper(i - 1, head.next);
}
public Node<E> getNode(int i) { // Wrapper Method
if (head == null)
return null;
else
return getNodeHelper(i, head);
}
// Method indexOf
private boolean indexOfHelper(E data, Node<E> tmp) { // Helper Method
if (tmp == null)
return false;
else if (tmp.data.equals(data))
return true;
tmp = tmp.next;
return indexOfHelper(data, tmp);
}
public boolean indexOf(E data) { // Wrapper Method
return indexOfHelper(data, head);
}
// Method remove
private boolean removeHelper(Node<E> head, Node<E> pred, E outData) { // Helper Method
if (head == null)
return false;
else if (head.data.equals(outData)) {
pred.next = head.next;
return true;
} else
return removeHelper(head.next, head, outData);
}
public boolean remove(E outData) { // Wrapper Method
if (head == null)
return false;
else if (head.data.equals(outData)) {
head = head.next;
return true;
} else
return removeHelper(head.next, head, outData);
}
// Method insert
public Node<E> insert(Node<E> head, int i) { // Wrapper Method
if (head == null)
head = new Node(i, null);
else
head.next = insert(head.next, i);
return head;
}
// Method insertBefore
private boolean insertBeforeHelper(E e1, E e2, Node<E> head) { // Helper Method
if (head.next == null)
return false;
else if (head.next.data.equals(e1)) {
Node<E> tmp = new Node<E>(e2);
tmp.next = head.next;
head.next = tmp;
return true;
} else
return insertBeforeHelper(e1, e2, head.next);
}
public boolean insertBefore(E e1, E e2) { // Wrapper Method
if (head == null)
return false;
else
return insertBeforeHelper(e1, e2, head);
}
// Method reverse
private Node<E> reverseHelper(Node<E> head, Node<E> prev) { // Helper Method
if (head == null)
return head;
else if (head.next == null) {
head.next = prev;
return head;
} else {
Node<E> next = head.next;
head.next = prev;
return reverseHelper(next, head);
}
}
public Node<E> reverse(Node<E> head) { // Wrapper Method
return reverseHelper(head, null);
}
// Method replace
private void replaceHelper(Node<E> head, E oldObj, E newObj) { // Helper Method
if (head != null) {
if (oldObj.equals(head.data))
head.data = newObj;
replaceHelper(head.next, oldObj, newObj);
}
}
public void replace(E oldObj, E newObj) { // Wrapper Method
replaceHelper(head, oldObj, newObj);
}
}