Class LinkedStack<T>
java.lang.Object
LinkedStack<T>
- Type Parameters:
T
- the type of data contained in the stack
- All Implemented Interfaces:
StackADT<T>
A generic singly-linked stack implementation.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns true if this stack contains the element, false otherwisegetList()
Creates a copy of the current contents of this stack in the order they are present here, in ArrayList form.boolean
isEmpty()
Returns true if this stack contains no elements.peek()
Accesses the value added to this stack most recently, without modifying the stackpop()
Removes and returns the value added to this stack most recentlyvoid
Add a new element to the top of this stack, assumed to be non-null.
-
Field Details
-
top
A reference to the LinkedNode currently at the top of the stack, which is null when the stack is empty.
-
-
Method Details
-
push
Description copied from interface:StackADT
Add a new element to the top of this stack, assumed to be non-null.- Specified by:
push
in interfaceStackADT<T>
- Parameters:
value
- the value to add
-
pop
Description copied from interface:StackADT
Removes and returns the value added to this stack most recently- Specified by:
pop
in interfaceStackADT<T>
- Returns:
- the most recently-added value, or null if the stack is empty
-
peek
Description copied from interface:StackADT
Accesses the value added to this stack most recently, without modifying the stack- Specified by:
peek
in interfaceStackADT<T>
- Returns:
- the most recently-added value, or null if the stack is empty
-
isEmpty
public boolean isEmpty()Description copied from interface:StackADT
Returns true if this stack contains no elements.- Specified by:
isEmpty
in interfaceStackADT<T>
- Returns:
- true if the stack contains no elements, false otherwise
-
contains
Description copied from interface:StackADT
Returns true if this stack contains the element, false otherwise- Specified by:
contains
in interfaceStackADT<T>
- Parameters:
value
- the value to check- Returns:
- true if the stack contains the element, false otherwise
-
getList
Creates a copy of the current contents of this stack in the order they are present here, in ArrayList form. This method should traverse the stack without removing any elements, and add the values (not the nodes!) to an ArrayList in the order they appear in the stack, with the top of the stack at index 0.- Returns:
- an ArrayList representation of the current state of this stack
-