-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
49 lines (42 loc) · 949 Bytes
/
stack.js
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
export { Stack }
class Stack{
constructor(){
this.size = 0;
this.buffer = 4;
this.stack = [];
}
clear(){
this.size = 0;
this.stack = [];
}
isEmpty(){
return ( this.size === 0 );
}
top(){
return this.stack[this.size-1];
}
pop(){
if(!this.isEmpty()) {
this.size--;
return this.stack.pop();
} else{
return [-1,''];
}
}
push(type, char){
if(this.isEmpty()){
if(type===0)
this.stack.push([type, char]);
} else{
let tmp = this.top();
if(tmp[0]===type && tmp[1].length < this.buffer){
let top = this.pop();
top[1] = char + top[1];
this.stack.push(top);
} else{
this.stack.push([type, char]);
}
}
this.size++;
}
}