Package components.stack
Class Stack2<T>
java.lang.Object
components.stack.StackSecondary<T>
components.stack.Stack2<T>
- Type Parameters:
T
- type of Stack entries
- All Implemented Interfaces:
Stack<T>
,StackKernel<T>
,Standard<Stack<T>>
,Iterable<T>
Stack
represented as a singly linked list, done "bare-handed", with
implementations of primary methods.
Execution-time performance of all methods implemented in this class is O(1).
- Representation Invariant (concrete invariant of $this):
$this.length >= 0 and if $this.length = 0 then [$this.top is null] else [$this.top is not null] and [$this.top points to the first node of a singly linked list containing $this.length nodes] and [next in the last node of that list is null]
- Abstraction Relation (interpretation mapping between $this and this):
this = [data in $this.length nodes starting at $this.top]
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
clear()
Resetsthis
to an initial value.iterator()
final int
length()
Reports length ofthis
.Returns a new object with the same dynamic type asthis
, having an initial value.final T
pop()
Removesx
from the top ofthis
.final void
Addsx
to the top ofthis
.final T
replaceTop
(T x) Replaces the top ofthis
withx
, and returns the old top.final T
top()
Reports the top ofthis
.final void
transferFrom
(Stack<T> source) Setsthis
to the incoming value ofsource
, and resetssource
to an initial value; the declaration notwithstanding, the dynamic type ofsource
must be the same as the dynamic type ofthis
.Methods inherited from class components.stack.StackSecondary
equals, flip, hashCode, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Stack2
public Stack2()No-argument constructor.
-
-
Method Details
-
newInstance
Description copied from interface:Standard
Returns a new object with the same dynamic type asthis
, having an initial value. If the typeT
has a no-argument constructor, then the value of the new returned object satisfies the contract of the no-argument constructor forT
. IfT
does not have a no-argument constructor, then the value of the new returned object satisfies the contract of the constructor call that was used to initializethis
.- Returns:
- new object "like"
this
with an initial value
-
clear
Description copied from interface:Standard
Resetsthis
to an initial value. If the typeT
has a no-argument constructor, thenthis
satisfies the contract of the no-argument constructor forT
. IfT
does not have a no-argument constructor, thenthis
satisfies the contract of the constructor call that was used to initialize#this
. -
transferFrom
Description copied from interface:Standard
Setsthis
to the incoming value ofsource
, and resetssource
to an initial value; the declaration notwithstanding, the dynamic type ofsource
must be the same as the dynamic type ofthis
. If the typeT
has a no-argument constructor, thensource
satisfies the contract of the no-argument constructor forT
. IfT
does not have a no-argument constructor, thensource
satisfies the contract of the constructor call that was used to initialize#source
.- Parameters:
source
- object whose value is to be transferred
-
push
Description copied from interface:StackKernel
Addsx
to the top ofthis
.- Parameters:
x
- the entry to be added
-
pop
Description copied from interface:StackKernel
Removesx
from the top ofthis
.- Returns:
- the entry removed
-
length
Description copied from interface:StackKernel
Reports length ofthis
.- Returns:
- the length of
this
-
iterator
-
top
Description copied from interface:Stack
Reports the top ofthis
. -
replaceTop
Description copied from interface:Stack
Replaces the top ofthis
withx
, and returns the old top.- Specified by:
replaceTop
in interfaceStack<T>
- Overrides:
replaceTop
in classStackSecondary<T>
- Parameters:
x
- the new top entry- Returns:
- the old top entry
-