forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleBrowser.go
56 lines (47 loc) · 964 Bytes
/
SimpleBrowser.go
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
package _8_stack
import "fmt"
type Browser struct {
forwardStack Stack
backStack Stack
}
func NewBrowser() *Browser {
return &Browser{
forwardStack: NewArrayStack(),
backStack: NewLinkedListStack(),
}
}
func (this *Browser) CanForward() bool {
if this.forwardStack.IsEmpty() {
return false
}
return true
}
func (this *Browser) CanBack() bool {
if this.backStack.IsEmpty() {
return false
}
return true
}
func (this *Browser) Open(addr string) {
fmt.Printf("Open new addr %+v\n", addr)
this.forwardStack.Flush()
}
func (this *Browser) PushBack(addr string) {
this.backStack.Push(addr)
}
func (this *Browser) Forward() {
if this.forwardStack.IsEmpty() {
return
}
top := this.forwardStack.Pop()
this.backStack.Push(top)
fmt.Printf("forward to %+v\n", top)
}
func (this *Browser) Back() {
if this.backStack.IsEmpty() {
return
}
top := this.backStack.Pop()
this.forwardStack.Push(top)
fmt.Printf("back to %+v\n", top)
}