[chuck-users] generating a stack
Scott Wheeler
wheeler at kde.org
Tue Dec 12 21:35:10 EST 2006
Spencer Salazar wrote:
> Hi Kijjasak,
> Yes, there is no stack functionality in ChucK at the moment. Your
> best option now is to use an array, although in the future native
> ChucK arrays will likely support push and pop stack operations.
>
Well, or naturally you can write your own stack. Here's a simple
integer stack:
class IntStack
{
class Item
{
0 => int value;
Item @ below;
}
new Item @=> Item @ bottom;
bottom @=> Item top;
fun void push(int value)
{
new Item @=> Item @ item;
value => item.value;
top @=> item.below;
item @=> top;
}
fun int pop()
{
top.value => int value;
if(top != bottom)
{
top.below @=> top;
}
return value;
}
fun int isEmpty()
{
return top == bottom;
}
}
Then the following code:
IntStack stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
while(!stack.isEmpty())
{
<<< stack.pop() >>>;
}
Produces:
5 :(int)
4 :(int)
3 :(int)
2 :(int)
1 :(int)
-Scott
More information about the chuck-users
mailing list