Nasal-Interpreter/stl/queue.nas

47 lines
883 B
Plaintext

# lib queue.nas
# valkmjolnir 2021/3/31
var queue=func()
{
var (begin,end)=(nil,nil);
return
{
push:func(elem)
{
var new_node=
{
elem:elem,
next:nil
};
if(begin==nil)
begin=end=new_node;
else
{
end.next=new_node;
end=new_node;
}
return;
},
pop:func()
{
if(begin!=nil)
begin=begin.next;
if(begin==nil)
end=nil;
},
front:func()
{
if(begin!=nil)
return begin.elem;
return nil;
},
clear:func()
{
begin=end=nil;
},
empty:func()
{
return begin==nil;
}
};
}