-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackObjct.html
More file actions
70 lines (59 loc) · 1.25 KB
/
StackObjct.html
File metadata and controls
70 lines (59 loc) · 1.25 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<script>
function Stack(){
this.dataStore = [];
this.top = 0;
}
Stack.prototype = {
push: function(element){
this.dataStore[this.top++] = element;
},
peek: function(){
return this.dataStore[this.top-1];
},
pop: function(){
return this.dataStore[--this.top];
},
clear: function(){
this.top = 0;
},
length: function(){
return this.top;
}
}
var s = new Stack();
s.push("David");
s.push("Raymond");
s.push("Bryan");
document.writeln("length: " + s.length());
document.writeln("</br>")
document.writeln(s.peek());
document.writeln("</br>")
var popped = s.pop();
document.writeln("The popped element is: " + popped);
document.writeln(s.peek());
document.writeln("</br>")
s.push("Cynthia");
document.writeln(s.peek());
document.writeln("</br>")
s.clear();
document.writeln("length: " + s.length());
document.writeln(s.peek());
document.writeln("</br>")
s.push("Clayton");
document.writeln(s.peek());
document.writeln("</br>")
function mulBase(num, base) {
var s = new Stack();
do {
s.push(num % base);
num = Math.floor(num /= base);
} while (num > 0);
var converted = "";
while (s.length() > 0) {
converted += s.pop();
}
return converted;
}
document.writeln(mulBase(10,2))
document.writeln("</br>")
</script>