-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1472.cpp
More file actions
33 lines (29 loc) · 882 Bytes
/
Copy path1472.cpp
File metadata and controls
33 lines (29 loc) · 882 Bytes
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
class BrowserHistory {
struct Node {
Node *next, *prev;
string val;
Node(string val = "#", Node *prev = nullptr, Node *next = nullptr)
: val(val), prev(prev), next(next) {}
};
Node *head = nullptr, *tail = nullptr, *crnt = nullptr;
public:
BrowserHistory(string homepage) { crnt = head = tail = new Node(homepage); }
void visit(string url) {
for (Node *t = tail->next; t;) {
Node *tmp = t;
t = t->next;
delete tmp;
}
crnt = tail = tail->next = new Node(url, tail, nullptr);
}
string back(int steps) {
while (steps-- && crnt->prev)
tail = crnt = crnt->prev;
return crnt->val;
}
string forward(int steps) {
while (steps-- && crnt->next)
tail = crnt = crnt->next;
return crnt->val;
}
};