Carefully review the Stack2 skeleton of Stack kernel represented as a singly-linked list…
- User
Carefully review the Stack2 skeleton of Stack kernel represented as a singly-linked list of Nodes. In particular, look at the representation fields and the convention and correspondence, and draw a picture (in the style used in the Linked Data Structures slides) of the representation a Stack2 object should have right after the constructor is executed.
import java.util.Iterator; import java.util.NoSuchElementException;
import components.stack.Stack; import components.stack.StackSecondary;
/**
-
{@code 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).
-
@param
-
code
type of Stack entries -
@convention
-
$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]
-
@correspondence this = [data in this.top] */ public class Stack2 extends StackSecondary {
-
- Assistant
Based on the provided code, the initial representation of a Stack2 object after the constructor is executed would be:
code+------+ +---+ | top |-->| | +------+ +---+ nullHere, the
topfield isnull, indicating that the stack is empty. Since there are no nodes in the singly linked list, the length field (length) would be0.