-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.js
More file actions
89 lines (79 loc) · 2.08 KB
/
Stack.js
File metadata and controls
89 lines (79 loc) · 2.08 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
// this.length = length;
this.clear = clear;
function push(element) {
this.dataStore[this.top++] = element;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top-1];
}
// function length() {
// return this.top;
// }
function clear() {
this.top = 0;
}
Object.defineProperty(this, 'length', {
get: function() {
return this.top;
}
});
}
var s = new Stack();
s.push('David');
s.push('Raymond');
s.push('Bryan');
console.log('length :', s.length, ', peek :', s.peek());
console.log('=============== pop ==============');
var popped = s.pop();
console.log('The popped element is : ', popped, ', peak :', s.peek());
s.push('Cynthia');
console.log(' peak :', s.peek());
console.log('============== clear =============');
s.clear();
console.log('length :', s.length, ', peek :', s.peek());
s.push('Clayton');
console.log(' peak :', s.peek());
// 회문 Palindrom (앞으로 읽으나 뒤로 읽으나 같은 단어, 구절, 숫자)
function isPalindrome(word) {
var s = new Stack();
for(i=0; i<word.length; i++) {
s.push(word[i]);
}
var rword = "";
while(s.length > 0) {
rword += s.pop();
}
return word == rword;
}
console.log('=========== Palindrome ===========');
var words = ['hello', 'racecar', '이효리', '1001'];
words.forEach (function(word) {
if( isPalindrome(word) )
console.log(word, 'is a palindrome')
else
console.log(word, 'is not a palindrome')
});
// 재귀
function factorial(n) {
if(n === 0) return 1;
else return n * factorial(n-1);
}
console.log('============ factorial ===========');
console.log( factorial(5) ); // 120
function factorialStack(n) {
var s = new Stack();
while(n > 1) s.push(n--);
var product = 1;
while(s.length > 0) product *= s.pop();
return product;
}
console.log( factorialStack(5) ); // 120