This commit is contained in:
Alexander Zhirov 2023-02-27 01:22:09 +03:00
parent 1d0a2964cf
commit 151d77ee54
11 changed files with 340 additions and 0 deletions

View file

@ -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);
}