Difference between revisions of "Atomic Stack Assignment"
Jump to navigation
Jump to search
(→empty) |
|||
| Line 30: | Line 30: | ||
[[File:Stack_edcba.svg]] | [[File:Stack_edcba.svg]] | ||
| + | |||
| + | In this state, <code>stack.peek()</code> will return <code>Optional.of("E")</code>. | ||
===push A, B, C, D, E, pop=== | ===push A, B, C, D, E, pop=== | ||
| + | <nowiki>Stack<String> stack = new NotThreadSafeStack<>(DefaultNode::new); | ||
| + | stack.push("A"); | ||
| + | stack.push("B"); | ||
| + | stack.push("C"); | ||
| + | stack.push("D"); | ||
| + | stack.push("E"); | ||
| + | Optional<String> optOfE = stack.pop();</nowiki> | ||
| + | |||
[[File:Stack_dcba.svg]] | [[File:Stack_dcba.svg]] | ||
| + | |||
| + | In this state, <code>stack.peek()</code> will return <code>Optional.of("D")</code>. | ||
=Code To Implement= | =Code To Implement= | ||
Revision as of 02:13, 10 November 2022
Contents
Node and Stack
Node
public interface Node<E> {
E value();
Optional<Node<E>> nextNode();
}
Stack
public interface Stack<E> {
void push(E value);
Optional<E> peek();
Optional<E> pop();
}
Example
empty
Stack<String> stack = new NotThreadSafeStack<>(DefaultNode::new);
In this state, stack.peek() will return Optional.empty() and stack.pop() will also return Optional.empty().
push A, B, C, D, E
Stack<String> stack = new NotThreadSafeStack<>(DefaultNode::new);
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
stack.push("E");
In this state, stack.peek() will return Optional.of("E").
push A, B, C, D, E, pop
Stack<String> stack = new NotThreadSafeStack<>(DefaultNode::new);
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
stack.push("E");
Optional<String> optOfE = stack.pop();
In this state, stack.peek() will return Optional.of("D").
Code To Implement
Node
public interface Node<E> {
E value();
Optional<Node<E>> nextNode();
}
DefaultNode
@Immutable class DefaultNode.
| class: | DefaultNode.java | |
| methods: | value nextNode |
|
| package: | stack.node.exercise | |
| source folder: | main/src/main/java |
constructor and instance variables
value
nextNode
Stack
public interface Stack<E> {
void push(E value);
Optional<E> peek();
Optional<E> pop();
}
NotThreadSafeStack
| class: | NotThreadSafeStack.java | |
| methods: | constructor nodeConstructor push peek pop |
|
| package: | stack.notthreadsafe.exercise | |
| source folder: | student/src/main/java |
constructor and instance variables
nodeConstructor
push
peek
pop
ConcurrentStack
| class: | ConcurrentStack.java | |
| methods: | constructor nodeConstructor push peek pop |
|
| package: | stack.concurrent.exercise | |
| source folder: | student/src/main/java |
constructor and instance variables
nodeConstructor
push
peek
pop
AtomicStack
| class: | AtomicStack.java | |
| methods: | constructor nodeConstructor push peek pop |
|
| package: | stack.atomic.exercise | |
| source folder: | student/src/main/java |
constructor and instance variables
nodeConstructor
push
peek
pop
Testing
| class: | StackTestSuite.java | |
| package: | stack.exercise | |
| source folder: | testing/src/test/java |
DefaultNode
| class: | _DefaultNodeTestSuite.java | |
| package: | stack.node.exercise | |
| source folder: | testing/src/test/java |
NotThreadSafeStack
| class: | _NotThreadSafeStackTestSuite.java | |
| package: | stack.notthreadsafe.exercise | |
| source folder: | testing/src/test/java |
ConcurrentStack
| class: | __ConcurrentStackTestSuite.java | |
| package: | stack.concurrent.exercise | |
| source folder: | testing/src/test/java |
sequential
| class: | _ConcurrentStackSequentialTestSuite.java | |
| package: | stack.concurrent.exercise | |
| source folder: | testing/src/test/java |
parallel
| class: | _ConcurrentStackParallelTestSuite.java | |
| package: | stack.concurrent.exercise | |
| source folder: | testing/src/test/java |
AtomicStack
| class: | __AtomicStackTestSuite.java | |
| package: | stack.atomic.exercise | |
| source folder: | testing/src/test/java |
sequential
| class: | _AtomicStackSequentialTestSuite.java | |
| package: | stack.atomic.exercise | |
| source folder: | testing/src/test/java |
parallel
| class: | _AtomicStackParallelTestSuite.java | |
| package: | stack.atomic.exercise | |
| source folder: | testing/src/test/java |