-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.js
More file actions
59 lines (46 loc) · 1.19 KB
/
Stack.js
File metadata and controls
59 lines (46 loc) · 1.19 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
// 참고자료 : https://www.youtube.com/watch?v=t2CEgPsws3U
//functon : push , pop, peek, length
//palindrome
let letters = []; //arrays is the same as stack
let word = "racecar";
let reverseWord = "";
//put letters of word into stack
for (let i = 0; i < word.length; i++) {
letters.push(word[i]);
}
//pop off the stack in reverse order
for (let i = 0; i < word.length; i++) {
reverseWord += letters.pop();
}
if (reverseWord === word) {
console.log(word + "is a palindrome.");
} else {
console.log(word + "is not a palindrome.");
}
//implement stack in this palindrome example
let Stack = funciton(){
this.count=0;
this.storage={};
//adds a value onto the end of the stack
this.push = function(value){
this.storage[this.count] = value;
this.count++
}
//removes and returns the value at the end of the stack
this.pop=function(){
if(this.count===0){
return undefined
}
this.count--;
let result = this.storage[this.count];
delete this.storage[this.count];
return result;
}
this.size = function(){
return this.count;
}
//return the value at the end of the stack
this.peak = function(){
return this.storage[this.count-1]
}
}