1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| 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; }
var s = new Stack(); s.push("Cynthia"); s.push("Raymond"); s.push("Barbara"); console.log("length:" + s.length()); console.log(s.peek()); var poped = s.pop(); console.log("The poped element is: " + poped); console.log(s.peek()); s.clear(); console.log("length:" + s.length());
|