// inserts the specified element at the specified position in this list. existing elements in the list are shifted right. If the specified index is at the end of list, the specified element is appended to the end of the list.
//@throws IndexOutOfBoundsException if index is invalid.
public void add(int index, E e){
public void add(int index, E e) {
if(index > listSize || index < 0){
throw new IndexOutOfBoundsException();
}
Node<E> n = new Node<E>(e);
if(index == 0){
n.next = head;
head = n;
if(listSize == 0){
tail = n;
}
}
else if(index == listSize){
tail.next = n;
tail = n;
}
else{
Node<E> s = head;
for(int i = 0; i<index-1; i++){
s = s.next;
}
n.next = s.next;
s.next = n;
}
listSize++;
}
//clear the entire list
public void clear(){
public void clear() {
tail = null;
head = null;
listSize = 0;
}
//check whether or not something is contained in the list
public boolean contains(E e){
public boolean contains(E e) {
return indexOf(e) != -1;
}
// return the element at a specified position in a list
public E get(int index){
public E get(int index) {
if(index<0 || index>=listSize){
throw new IndexOutOfBoundsException();
}
Node<E> n = head;
for(int i = 0; i<index; i++){
n = n.next;
}
return n.data;
}
//find the index of a specific element in the list
public int indexOf(E e){
public int indexOf(E e) {
Node<E> n = head;
int pos = 0;
while(n!= null){
if(n.data.equals(e)){
return pos;
}
pos++;
n = n.next;
}
return -1;
}
//check if the list is empty
public boolean is Empty(){
public boolean isEmpty() {
return listSize ==0;
}
//find the last index of an element in a list
public int lastIndexOf(E e){
public int lastIndexOf(E e) {
Node<E> n = head;
int pos = 0;
int ret = -1;
while(n!=null){
if(n.data.equals(e)){
ret = pos;
}
pos++;
n=n.next;
}
return ret;
}
//remove the element at a specific position of a list
public E remove (int index){
public E remove(int index) {
if (index < 0 || index >= listSize) {
throw new IndexOutOfBoundsException();
}
E returnVal = null;
// deleting the only element in the list
if (listSize == 1) {
returnVal = head.data;
head = null;
tail = null;
}
// deleting first element of list having at least 2 elements
else if (index == 0) {
returnVal = head.data;
head = head.next;
} else {
Node<E> n = head;
for (int i = 0; i < index - 1; i++) {
n = n.next;
}
returnVal = n.next.data;
n.next = n.next.next;
// If deleting last node in list, change tail reference.
if (index == listSize - 1) {
tail = n;
}
}
listSize--;
return returnVal;
}
//check if you need to remove an element in a list and remove it
public boolean remove(E e){
public boolean remove(E e) {
int index = indexOf(e);
if(index>=0){
remove(index);
return true;
}
else{
return false;
}
}
//set a specific value to a specific index
public E set(int index, E e){
public E set(int index, E e) {
if(index<0 || index>= listSize){
throw new IndexOutOfBoundsException();
}
Node<E> n = new Node(e);
for (int i = 0; i < index; i++) {
n=n.next;
}
E ret = n.data;
n.data = e;
return ret;
}
//return the size of the list
public int size(){
public int size() {
return listSize;
}
public void add(E e)
public void add(E e) {
add(listSize, e);
}