class Stack #( ItemType: type of any )
{
private:
buffer: ItemType[];
stack_size: int;
stack_top_index: int;
public:
function push (new_item: ItemType)
{ if (stack_top_index < stack_size)
buffer[++stack_top_index]:=new_item;
}
function pop ():ItemType
{ if (stack_top_index >=0)
return buffer[stack_top_index--];
return 0;
}
enter (size: int) // constructor
{ if (size<100) size:=100;
buffer := new ItemType[size];
stack_size := size;
stack_top_index := -1;
};
};
The Stack class is parameterized by a class parameter, ItemType, which can be a subclass of any, which is the root class. For how to use the parameterized class, see this example.
More examples: