Before adding B
CSC 385 - Data Structures and Algorithms
University of Illinois Springfield
College of Health, Science, and Technology
public interface List<T> {
/*Returns item at index*/
public T get(int index);
/*Adds obj to position index*/
public boolean add(int index, T obj);
/*Sets item at index to obj*/
public T set(int index, T obj);
/*Removes item at index*/
public boolean remove(int index);
/*Removes first occurrence of obj*/
public boolean remove(T obj);
/*Returns the index of the first occurrence
of obj in the list*/
public int indexOf(T obj);
}prepend: Adds elements to the front of the list like a Queueappend: Adds elements to the back of the list like a stackinsert: Adds elements in the middle of the listadd but sometimes insert is the name used in other languagesremoveFront: Removes and returns the element at the front of the list similar to dequeueremoveLast: Removes and returns the last element of the list similar to popT[] array: Array of T objectssize: An integer that counts the number of entriesINITIAL_CAPACITY: Used to store initial capacity of the array| index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| 10 | 20 | 30 | 40 | null | null | null |
size might be valid or invalid.| index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| 10 | 20 | 30 | 40 | null | null | null |
get(int index)
indexOf(T obj)
contains(T obj)
indexOf method to do a sequential searchindexOf does returns anything other than -1)indexOf this is also \(O(n)\)set(int index, T obj)
add(int index, T obj)
size == array.length then resize() is called
append(T obj)
size == array.length then resize() is calledprepend(T obj)
size == array.length then resize() is calledarray to the new arrayremove(int index)
remove(T obj)
indexOf method to get first occurrence of obj if it existsremove(index)
size: Size of the listhead: Node that contains the element at the front of the listtail: Node that contains the element at the back of the listfindObjectNode(T obj): Traverses the linked list until it finds the node that contains the given item and returns the node else returns null. This must perform a linear search so is \(O(n)\).getNodeAt(int index): Retrieves the node at the given indexget()
get(int index)
getNodeAt(index) method to retrieve the nodeindexOf(T obj)
findObjectNode except returns the index of the obj or -1 if it does not existcontains(T obj)
findObjectNode and returns true if anything other than null is returned else falseadd(int index, T obj)
getNode(index) to retrieve nodeBefore adding B
After adding B
removeNode(node)
Node B To Be Removed
Node B Removed
remove(int index)
getNode(node) and then pass it to remove(node)remove(T obj)
findObjNode(obj) to retrieve the node containing objremove(node)findObjNodeset(int index, T obj)
getNode(index) to retrieve node at index
CSC 385 - Data Structures and Algorithms