Carefully review the Queue2 implementation of Queue kernel represented as a singly-linked list of Nodes. In particular, look at the representation fields (and the convention and correspondence) and the code in createNewRep and the constructor, and draw a picture (in the style used in the Linked Data Structures slides) of the Queue2 representation right after the constructor is executed. 2. 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. 3. Complete the implementation of the Stack kernel represented as a singly-linked list of Nodes in the skeleton provided, Stack2. You need to write bodies for the private method createNewRep and for the kernel methods push, pop, and length. Note that although the Stack2 implementation is similar to the Queue2 implementation, there are two significant differences: (1) the Queue2 representation has three fields (instance variables): preFront, rear, and length, while Stack2 only has two fields: top and length; (2) the singly-linked list representing Queue2 has one extra "smart" node at the front of the list, while the singly-linked list representing Stack2 does not include this extra node. 4. Develop a complete test plan for the Stack2 constructor and kernel methods you implemented and enter them in StackTest. package components.queue; import java.util.Iterator; import java.util.NoSuchElementException; /** * {@code Queue} 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 * type of {@code Queue} entries * @convention * this.lenthis.preFront is not null] and * [this.preFront points to the first node of a singly linked list * containing this.rear points to the last node in that singly linked list] and * [this.preFront.next and * running through this.length >= 0 and * if this.top is null] * else * [this.top points to the first node of a singly linked list * containing this.length nodes starting at $this.top] * Private members -------------------------------------------------------- * * Node class for singly linked list nodes. * * Data in node. * * Next node in singly linked list, or null. * * Top node of singly linked list. * * Number of nodes in singly linked list, i.e., length = |this|. * * Creator of initial representation. * Constructors ----------------------------------------------------------- * * No-argument constructor. * Standard methods removed to reduce clutter... * Kernel methods --------------------------------------------------------- * Iterator code removed to reduce clutter... * * JUnit test fixture for {@code Stack}'s constructor and kernel * methods. * * @author Put your name here * * * Invokes the appropriate {@code Stack} constructor for the implementation * under test and returns the result. * * @return the new stack * @ensures constructorTest = <> * * Invokes the appropriate {@code Stack} constructor for the reference * implementation and returns the result. * * @return the new stack * @ensures constructorRef = <> * * * Creates and returns a {@code Stack} of the implementation under * test type with the given entries. * * @param args * the entries for the stack * @return the constructed stack * @ensures createFromArgsTest = [entries in args] * * * Creates and returns a {@code Stack} of the reference * implementation type with the given entries. * * @param args * the entries for the stack * @return the constructed stack * @ensures createFromArgsRef = [entries in args] */ private Stack createFromArgsRef(String... args) { Stack stack = this.constructorRef(); for (String s : args) { stack.push(s); } stack.flip(); return stack; } // TODO - add test cases for constructor, push, pop, and length }
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
/ private void createNewRep() { // TODO - fill in body } /
/ /
/ public Stack2() { this.createNewRep(); } /
/ /
/ @Override public final void push(T x) { assert x != null : "Violation of: x is not null"; // TODO - fill in body } @Override public final T pop() { assert this.length() > 0 : "Violation of: this /= <>"; // TODO - fill in body } @Override public final int length() { // TODO - fill in body } /
The Queue2 implementation of Queue kernel is represented as a singly-linked list of Nodes. It has three representation fields: preFront, rear, and length. The preFront field points to a "smart" node before the front node of the list. The rear field points to the last node in the list. The length field represents the number of nodes in the list.
Right after the constructor is executed, the Queue2 representation would look like this:
preFront rear
| |
---- node1 ---- ---- node2 ---- ---- node3 ----
| data | | data | | data |
| next ------------> next ------------> null |
--------------- --------------- ---------------
The Stack2 skeleton of Stack kernel is similar to the Queue2 implementation, but it does not include the extra "smart" node at the front of the list. It has two representation fields: top and length. The top field points to the top node of the list and the length field represents the number of nodes in the list.
Right after the constructor is executed, the Stack2 representation would look like this:
top
|
---- node1 ---- ---- node2 ---- ---- node3 ----
| data | | data | | data |
| next ------------> next ------------> null |
--------------- --------------- ---------------
Implementation of Stack2 kernel methods:
@Override
public final void push(T x) {
assert x != null : "Violation of: x is not null";
Node p = new Node();
p.data = x;
p.next = this.top;
this.top = p;
this.length++;
}
@Override
public final T pop() {
assert this.length() > 0 : "Violation of: this /= <>";
T result = this.top.data;
this.top = this.top.next;
this.length--;
return result;
}
@Override
public final int length() {
return this.length;
}
Test plan for Stack2 constructor and kernel methods:
Test constructorTest:
Create a Stack using constructorTest.
Check that the length of the Stack is 0.
Test push:
Create a Stack using constructorTest.
Push multiple elements into the Stack.
Check that the length of the Stack is equal to the number of elements pushed.
Test pop:
Create a Stack using constructorTest.
Push an element into the Stack.
Pop the element from the Stack.
Check that the length of the Stack is 0.
Test length:
Create a Stack using constructorTest.
Push multiple elements into the Stack.
Check that the length of the Stack is equal to the number of elements pushed.