forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1472.cpp
More file actions
64 lines (42 loc) · 1.32 KB
/
1472.cpp
File metadata and controls
64 lines (42 loc) · 1.32 KB
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
60
61
62
63
64
//create doubly linked list
struct LstNode{
LstNode* next, *prev;
string val;
LstNode() : val(""), next(nullptr), prev(nullptr) {}
LstNode(string s) : val(s), next(nullptr), prev(nullptr) {}
LstNode(string s, LstNode* next, LstNode* prev) : val(s), next(next), prev(prev) {}
};
class BrowserHistory {
LstNode* node = new LstNode("");
LstNode* result = node;
public:
BrowserHistory(string homepage) {
LstNode* n1 = new LstNode(homepage);
node->next = n1;
n1->prev = node;
node = n1;
}
void visit(string url) {
LstNode* n2 = new LstNode(url);
node->next = n2;
n2->prev = node;
node = n2;
}
string back(int steps) {
while(node->prev != result && steps--)
node = node->prev;
return node->val;
}
string forward(int steps) {
while(steps-- && node->next)
node = node->next;
return node->val;
}
};
/**
* 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);
*/