-
Notifications
You must be signed in to change notification settings - Fork 6
/
browser_history.cpp
51 lines (42 loc) · 1.17 KB
/
browser_history.cpp
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
class BrowserHistory {
public:
stack<string> history;
stack<string> future;
BrowserHistory(string homepage) {
history.push(homepage);
future = stack<string>(); // Reset the forward stack.
}
void visit(string url) {
history.push(url);
future = stack<string>(); // Reset the forward stack.
}
string back(int steps) {
while(steps > 0 && history.size() > 1) { // Always keep at least one element in the stack.
future.push(history.top());
history.pop();
steps--;
}
return history.top();
}
string forward(int steps) {
while(steps > 0 && future.size() > 0) {
history.push(future.top());
future.pop();
steps--;
}
return history.top();
}
};
/**
* 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);
*/
/*
>start on homepage
>visit a url
>go back -- number of steps
>move forward -- number of steps
*/