fix stack

This commit is contained in:
Matthew 2025-09-06 09:00:37 +10:00
parent 86f94df063
commit 3046baee78

26
util.d
View File

@ -198,31 +198,31 @@ DLLPush(T, U)(T* list, U* node, U* nil)
struct Stack(T)
{
Node!(T)* first;
Node!(T)* top;
}
void
SPush(T, U)(T* stack, U* node, U* nil)
SPush(T)(Stack!(T)* stack, Node!(T)* node, Node!(T)* nil)
{
if (CheckNil(nil, stack.first))
if (CheckNil(nil, stack.top))
{
stack.first = node;
stack.top = node;
node.next = nil;
}
else
{
node.next = stack.first;
stack.first = node;
node.next = stack.top;
stack.top = node;
}
}
U*
SPop(T, U)(T* stack, U* nil)
Node!(T)*
SPop(T)(Stack!(T)* stack, Node!(T)* nil)
{
U* node = stack.first;
if (!CheckNil(nil, stack.first))
Node!(T)* node = stack.top;
if (!CheckNil(nil, stack.top))
{
stack.first = stack.first.next;
stack.top = stack.top.next;
}
return node;
}
@ -976,7 +976,7 @@ unittest
SPush(&stack, &n3, null);
u32 count = 3;
for (auto n = stack.first; !CheckNil(null, n); n = n.next, count -= 1)
for (auto n = stack.top; !CheckNil(null, n); n = n.next, count -= 1)
{
assert(n.value == count);
}
@ -987,6 +987,6 @@ unittest
assert(n.value == count);
}
assert(stack.first == null);
assert(stack.top == null);
}
}