Skip to content

Commit 287a1f3

Browse files
author
Carlos Green
committed
algos
1 parent 9e2c11d commit 287a1f3

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Chapter4Stacks/Chapter1/chap1-1.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

Chapter4Stacks/chap4-1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ function toString() {
3838
}
3939
}
4040
return retstr;
41-
}
41+
}

Chapter4Stacks/stack.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function Stack() {
2+
this.dataStore = []
3+
this.top = 0
4+
this.push = (element) => {
5+
this.dataStore[this.top++] = element
6+
}
7+
this.pop = () => {
8+
return this.dataStore[--this.top]
9+
}
10+
this.peek = () => {
11+
return this.dataStore[this.top-1]
12+
}
13+
this.length = () => {
14+
return this.top
15+
}
16+
this.clear = () => {
17+
for (let i = 0; i < this.top; i++) {
18+
this.dataStore.pop()
19+
}
20+
this.top = 0
21+
console.log(this.dataStore, 'data')
22+
}
23+
}
24+
25+
let st1 = new Stack()
26+
27+
st1.push('nice')
28+
st1.push('cool')
29+
st1.push('sweet')
30+
st1.push('nice')
31+
32+
console.log(st1.dataStore, 'store')
33+
st1.clear()
34+
console.log(st1.dataStore, 'store')
35+
st1.push('nice')
36+
console.log(st1.dataStore, 'store')

0 commit comments

Comments
 (0)