<script type="text/javascript">
function Stack()
{
this. dataStore = [] ;
this. top = 0;
this. push = push;
this. pop = pop;
this. peek = peek;
this. clear = clear;
this. length = length;
}
function push(element)
{
this. dataStore[this. top++] = element;
}
function peek()
{
return this. dataStore[this. top- 1] ;
}
function pop()
{
return this. dataStore[- - this. top] ;
}
function clear()
{
this. top = 0;
}
function length()
{
return this. top;
}
function doTest()
{
var s = new Stack();
s. push(" David" );
s. push(" Raymond" );
s. push(" Bryan" );
document.write(" length: " + s. length());
document.write(s. peek());
var popped = s. pop();
document.write(" The popped element is: " + popped);
document.write(s. peek());
s. push(" Cynthia" );
document.write(s. peek());
s. clear();
document.write(" length: " + s. length());
document.write(s. peek());
s. push(" Clayton" );
document.write(s. peek());
}
</script>
js实现栈
最新推荐文章于 2022-04-10 19:56:36 发布