-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1472. Design Browser History.java
59 lines (59 loc) · 1.68 KB
/
1472. Design Browser History.java
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
class BrowserHistory {
private static class NodeHead {
String url;
NodeHead previous;
NodeHead next;
public NodeHead(String url, NodeHead previous, NodeHead next) {
super();
this.url = url;
this.previous = previous;
this.next = next;
}
}
private NodeHead urlsQueue;
public BrowserHistory(String homepage) {
NodeHead currentSite = new NodeHead(homepage, null, null);
urlsQueue = currentSite;
}
public void visit(String url) {
NodeHead currentSite = new NodeHead(url, urlsQueue, null);
urlsQueue.next = currentSite;
urlsQueue = currentSite;
}
public String back(int steps) {
NodeHead current = urlsQueue;
while (current.previous != null && steps > 0) {
current = current.previous;
steps -= 1;
}
if (current==null) return null;
urlsQueue=current;
return current.url;
}
public String forward(int steps) {
NodeHead current = urlsQueue;
while (current.next != null && steps > 0) {
current = current.next;
steps -= 1;
}
if (current==null) return null;
urlsQueue=current;
return current.url;
}
}
/**
* Your BrowserHistory object will be instantiated and called as such:
* BrowserHistory obj = new BrowserHistory(homepage);
* obj.visit(url);
* String param_2 = obj.back(steps);
* String param_3 = obj.forward(steps);
*/