js实现栈

来源:互联网 发布:松下fpwin编程手册 编辑:程序博客网 时间:2024/06/05 20:16
<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>

0 0
原创粉丝点击