sources
This commit is contained in:
parent
1d0a2964cf
commit
151d77ee54
11 changed files with 340 additions and 0 deletions
|
@ -0,0 +1,50 @@
|
|||
import std.array;
|
||||
|
||||
interface Stack(T)
|
||||
{
|
||||
@property bool empty();
|
||||
@property ref T top();
|
||||
void push(T value);
|
||||
void pop();
|
||||
}
|
||||
|
||||
class StackImpl(T) : Stack!T
|
||||
{
|
||||
private T[] _store;
|
||||
|
||||
@property bool empty()
|
||||
{
|
||||
return _store.empty;
|
||||
}
|
||||
|
||||
@property ref T top()
|
||||
{
|
||||
assert(!empty);
|
||||
return _store.back;
|
||||
}
|
||||
|
||||
void push(T value)
|
||||
{
|
||||
_store ~= value;
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
assert(!empty);
|
||||
_store.popBack();
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
auto stack = new StackImpl!int;
|
||||
assert(stack.empty);
|
||||
stack.push(3);
|
||||
assert(stack.top == 3);
|
||||
stack.push(5);
|
||||
assert(stack.top == 5);
|
||||
stack.pop();
|
||||
assert(stack.top == 3);
|
||||
stack.pop();
|
||||
assert(stack.empty);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue