Skip to content

Commit 5bcedfc

Browse files
committed
1472. Design Browser History
Signed-off-by: rajput-hemant <[email protected]>
1 parent 4182fa3 commit 5bcedfc

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
type BrowserHistory struct {
4+
histories []string
5+
currentPage int
6+
}
7+
8+
func Constructor(homepage string) BrowserHistory {
9+
return BrowserHistory{
10+
histories: []string{homepage},
11+
currentPage: 0,
12+
}
13+
}
14+
15+
func (this *BrowserHistory) Visit(url string) {
16+
if this.currentPage != len(this.histories)-1 {
17+
this.histories = append(this.histories[:this.currentPage+1], []string{}...)
18+
}
19+
20+
this.histories = append(this.histories, url)
21+
this.currentPage++
22+
}
23+
24+
func (this *BrowserHistory) Back(steps int) string {
25+
for i := this.currentPage; 0 <= i; i-- {
26+
if steps == 0 || this.currentPage == 0 {
27+
break
28+
}
29+
30+
this.currentPage--
31+
steps--
32+
}
33+
34+
return this.histories[this.currentPage]
35+
}
36+
37+
func (this *BrowserHistory) Forward(steps int) string {
38+
lenHistories := len(this.histories)
39+
40+
for i := this.currentPage; i < lenHistories; i++ {
41+
if steps == 0 || this.currentPage == lenHistories-1 {
42+
break
43+
}
44+
45+
this.currentPage++
46+
steps--
47+
}
48+
49+
return this.histories[this.currentPage]
50+
}

0 commit comments

Comments
 (0)