CSCD 211
Create a toString class for head and tail node
@Override public String toString(){ StringBuilder result = new StringBuilder("{"); Node<E> curr = head; for( int i = 0; i < size; i++){ result.append(curr.element); curr = curr.next; if(curr != null) result.append(", "); else reult.append("}"); } return result.toString(); }
Create a lastIndexOf method for head and tail node
lastIndexOf(E e){ int index = -1; Node<E> current = head; for(int i = 0; i < size; i++){ if (current.element.equals(e)) index = 1; current = current.next; } return -1; }
Create a LinkedListIterator method for head and tail node
private class LinkedListIterator implements Iterator{ private Node<E> current = head; public boolean hasNext(){ return (current != null); } public E next(){ E e = current.element; current = current.next; return e; } public void remove(){ } }
Create a get method for head and tail node
public E get(int index){ Node<E> current = head; for(int i = 0; i < index; i++){ current = current.next; } return current.element; }
Create a remove class for head and tail node
public E remove(int index){ if(index < 0 || index >= size){ return null; else if (index == size - 1){ return removeLast(); else{ Node<E> previous = head; for(int i =1; i < index; i++) previous = previous.next; Node<E> current = previous.next; previous.next = current.next; size--; return current.element; } }
Create a removeFirst class for head and tail node
public E removeFirst(){ if(size == 0){ return null; } else{ E temp = head.element; head = head.next; size--; if(head == null){ tail = null; } return temp; } }
Create a removeLast class for head and tail node
public E removeLast(){ if(size == 0) return null; else if (size == 1){ E temp = head.element; head = tail = null; size = 0; return temp; } else{ Node<E> current = head; for(int i = 1; i < size - 1; i++, current = current.next){ } E temp = tail.element; tail = current; tail.next = null; size--; return temp; } }
Create a set method for head and tail node
public E set(int index, E e){ Node<E> current = head; for(int i = 0; i < index; i++){ current = current.next; } E oldValue = current.element; current.element = e; return oldValue; }
Create an Iterator method for head and tail node
public Iterator<E> iterator(){ return new LinkedListIterator(); }
Create a contains class for head and tail node
public boolean contains(Object e){ return indexOf(e) > -1; }
Create an indexOf method for head and tail node
public int indexOf(Object e){ Node<E> current = head; for(int i = 0; i < size; i++){ if(current.element.equals(e)) return i; current = current.next; } return -1; }
Create an addclass for head and tail node
public void add(int index, E e){ if(index == 0) addFirst(e); else if (index >= size) addLast(e); else{ Node<E> current = head; for(int i = 1; i < index; i++){ current = current.next; } Node<E> temp = current.next; current.next = new Node<>(e); (current.next).next = temp; size++; } }
Create an addFirst class for head and tail node
public void addFirst(E e){ Node<E> newNode = new Node<>(e); newNode.next = head; head = newNode; size++ if(tail == null) tail = head; }
Create an addLast class for head and tail node
public void addLast(E e){ Node<E> newNode = new Node<>(e); if (tail == null) head = tail = newNode; else{ tail.next = newNode; tail = newNode; } size++; }
Create a clear class for head and tail node
public void clear(){ size = 0; head = tail = null; }