Class SLinked_ListOfString
java.lang.Object
SLinked_ListOfString
- All Implemented Interfaces:
ListOfString_Interface
Singly linked list of Strings
- Author:
- Dr. Lillis
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate SNode_StringReference to the first element in this linked list.private intNumber of elements currently stored in this linked list.private SNode_StringReference to the last element in this linked list. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the given element at the given index position i.voidAdds the given element to the end of this list.voidAdds the given element to the beginning of this list.voidAdds the given element to the end of this list.voidclear()Removes all list elements, if any exist.booleanReturns true if the specified element in in this list, returns false otherwise.intfirstIndexOf(String str) Returns the index of the first occurrence of the specified element, -1 if the element is not in this listget(int i) Returns the element at the given index position.getFirst()Returns the first element in this list.getLast()Returns the last element in this list.booleanisEmpty()Returns true if this list is empty, returns false otherwise.intlastIndexOf(String str) Returns the index of the last occurrence of the specified element, -1 if the element is not in this list not found.remove(int i) Removes and returns the element at the specified index position in the list.Removes and returns the first occurrence of the specified element.Returns null if the element is not in this list.Removes and returns the first element in this list.Removes and returns the last element in this list.Replaces the element at the given index position with the given element and returns the old element.Replaces the first element with the given element and returns the old element.Replaces the last element with the given element and returns the old element.intsize()Returns the number of elements in this list.toString()Returns a string containing each element in this list, separated by commas, enclosed in square brackets.
-
Field Details
-
head
Reference to the first element in this linked list. -
tail
Reference to the last element in this linked list. -
size
private int sizeNumber of elements currently stored in this linked list.
-
-
Constructor Details
-
SLinked_ListOfString
public SLinked_ListOfString()Constructs an empty list. The head and tail are both set to null and size is set to zero.
-
-
Method Details
-
add
Description copied from interface:ListOfString_InterfaceAdds the given element at the given index position i.- Specified by:
addin interfaceListOfString_Interface- Parameters:
i- index position where the given element will be addedstr- element to be added at the given index position- Throws:
IndexOutOfBoundsException- if i < 0 or i > size() Note: that it is OK for i to equal size()
-
addFirst
Description copied from interface:ListOfString_InterfaceAdds the given element to the beginning of this list.- Specified by:
addFirstin interfaceListOfString_Interface- Parameters:
str- element to be added to the beginning of this list
-
addLast
Description copied from interface:ListOfString_InterfaceAdds the given element to the end of this list.- Specified by:
addLastin interfaceListOfString_Interface- Parameters:
str- element to be added to the end of this list
-
add
Description copied from interface:ListOfString_InterfaceAdds the given element to the end of this list.- Specified by:
addin interfaceListOfString_Interface- Parameters:
str- element to be added to the end of this list
-
remove
Description copied from interface:ListOfString_InterfaceRemoves and returns the element at the specified index position in the list.- Specified by:
removein interfaceListOfString_Interface- Parameters:
i- index of element to be removed and returned- Returns:
- element stored at the given index position
- Throws:
IndexOutOfBoundsException- if i < 0 or i ≥ size()
-
removeFirst
Description copied from interface:ListOfString_InterfaceRemoves and returns the first element in this list.- Specified by:
removeFirstin interfaceListOfString_Interface- Returns:
- first element in this list
- Throws:
IllegalStateException- if this list is empty.
-
removeLast
Description copied from interface:ListOfString_InterfaceRemoves and returns the last element in this list.- Specified by:
removeLastin interfaceListOfString_Interface- Returns:
- last element in this list
- Throws:
IllegalStateException- if this list is empty.
-
remove
Description copied from interface:ListOfString_InterfaceRemoves and returns the first occurrence of the specified element.Returns null if the element is not in this list.- Specified by:
removein interfaceListOfString_Interface- Parameters:
str- element to be removed from this list- Returns:
- the element that was removed
-
get
Description copied from interface:ListOfString_InterfaceReturns the element at the given index position.- Specified by:
getin interfaceListOfString_Interface- Parameters:
i- index of the list element to return- Returns:
- the element located at the given index position
- Throws:
IndexOutOfBoundsException- if i < 0 or i ≥ size()
-
getFirst
Description copied from interface:ListOfString_InterfaceReturns the first element in this list.- Specified by:
getFirstin interfaceListOfString_Interface- Returns:
- the first element in this list.
- Throws:
IllegalStateException- if list is empty.
-
getLast
Description copied from interface:ListOfString_InterfaceReturns the last element in this list.- Specified by:
getLastin interfaceListOfString_Interface- Returns:
- the last element in this list
- Throws:
IllegalStateException- if this list is empty.
-
set
Description copied from interface:ListOfString_InterfaceReplaces the element at the given index position with the given element and returns the old element.- Specified by:
setin interfaceListOfString_Interface- Parameters:
i- index of the element to be replacedstr- new element that will be stored at the given index position- Returns:
- element that was previously stored at the given index position
- Throws:
IndexOutOfBoundsException- if i < 0 or i ≥ size()
-
setFirst
Description copied from interface:ListOfString_InterfaceReplaces the first element with the given element and returns the old element.- Specified by:
setFirstin interfaceListOfString_Interface- Parameters:
str- new element that will be stored as the first list element- Returns:
- element that was previously stored as the first list element
- Throws:
IllegalStateException- if list is empty.
-
setLast
Description copied from interface:ListOfString_InterfaceReplaces the last element with the given element and returns the old element.- Specified by:
setLastin interfaceListOfString_Interface- Parameters:
str- new element that will be stored as the last list element- Returns:
- element that was previously stored as the last list element
- Throws:
IllegalStateException- if list is empty.
-
isEmpty
public boolean isEmpty()Description copied from interface:ListOfString_InterfaceReturns true if this list is empty, returns false otherwise.- Specified by:
isEmptyin interfaceListOfString_Interface- Returns:
- true if this list is empty, returns false otherwise
-
contains
Description copied from interface:ListOfString_InterfaceReturns true if the specified element in in this list, returns false otherwise.- Specified by:
containsin interfaceListOfString_Interface- Parameters:
str- element that is being searched for- Returns:
- true if the specified element is in this list, returns false otherwise
-
firstIndexOf
Description copied from interface:ListOfString_InterfaceReturns the index of the first occurrence of the specified element, -1 if the element is not in this list- Specified by:
firstIndexOfin interfaceListOfString_Interface- Parameters:
str- element being searched for- Returns:
- index of the first occurrence of the specified element, returns -1 if the element is not in this list
-
lastIndexOf
Description copied from interface:ListOfString_InterfaceReturns the index of the last occurrence of the specified element, -1 if the element is not in this list not found.- Specified by:
lastIndexOfin interfaceListOfString_Interface- Parameters:
str- element being searched for- Returns:
- index of the last occurrence of the specified element, returns -1 if the element is not in this list
-
size
public int size()Description copied from interface:ListOfString_InterfaceReturns the number of elements in this list.- Specified by:
sizein interfaceListOfString_Interface- Returns:
- number of elements in this list
-
clear
public void clear()Description copied from interface:ListOfString_InterfaceRemoves all list elements, if any exist.- Specified by:
clearin interfaceListOfString_Interface
-
toString
Description copied from interface:ListOfString_InterfaceReturns a string containing each element in this list, separated by commas, enclosed in square brackets. For example, if a list contained the three elements "A", "B", and "C", then this method would return the String "[A, B, C"].- Specified by:
toStringin interfaceListOfString_Interface- Overrides:
toStringin classObject- Returns:
- a string representation of this list.
-