Skip to content

Instantly share code, notes, and snippets.

@atuttle
Created February 26, 2010 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuttle/316000 to your computer and use it in GitHub Desktop.
Save atuttle/316000 to your computer and use it in GitHub Desktop.
<cfcomponent hint="A stack class that makes working with arrays as stacks clean and simple">
<cfscript>
//private data
variables.data = arrayNew(1);
function init(){
if (!structIsEmpty(arguments)){
if (isArray(arguments[1])){//handle array source data
variables.data = arguments[1];
}else if (isSimpleValue(arguments[1])){//handle string source data
variables.data = listToArray(arguments[1]);
}
}
return( this );
}
function push(newVal){
arrayAppend(variables.data, arguments.newVal);
}
function pop(){
var rtn = '';
if (isEmpty())
return '';
rtn = variables.data[arrayLen(variables.data)];
arrayDeleteAt(variables.data, arrayLen(variables.data));
return( rtn );
}
function top(){
if (!isEmpty())
return( variables.data[arrayLen(variables.data)] );
else
return '';
}
function isEmpty(){
return( arrayLen(variables.data) == 0 );
}
</cfscript>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment