-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample-browser.js
51 lines (45 loc) · 1.13 KB
/
sample-browser.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
50
51
const stack = require("./stack-based-on-linked-list");
console.log(stack, "stack");
class SampleBrowser {
constructor() {
this.normalStack = new stack.StackBasedLinkedList();
this.backStack = new stack.StackBasedLinkedList();
}
pushNormal(name) {
this.normalStack.push(name);
this.backStack.clear();
}
back() {
const value = this.normalStack.pop();
if(value !== -1) {
this.backStack.push(value);
this.displayAllStack();
} else {
console.log('无法后退');
}
}
front() {
const value = this.backStack.pop();
if(value !== -1) {
this.normalStack.push(value);
this.displayAllStack();
} else{
console.log('无法前进');
}
}
// 打印栈内数据
displayAllStack() {
console.log("---后退页面---");
this.backStack.display();
console.log("---浏览页面---");
this.normalStack.display();
}
}
const browser = new SampleBrowser();
browser.pushNormal('www.google.com');
browser.pushNormal('www.baidu.com');
browser.pushNormal('www.github.com');
browser.back();
browser.back();
browser.front();
browser.pushNormal('www.new.com');