aboutsummaryrefslogtreecommitdiff
path: root/browser_history.cpp
blob: df7d3cd5f28e7fc266fada5ae7a7855f6fb31ce8 (plain)
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
// 1472. Design Browser History
#include "leetcode.h"

/*
 * you have a browser of one tab where you start on the homepage and can visit
 * another url, get back in the history number of steps or move forward in the
 * history number of steps. implement BrowserHistory class (they're self
 * explantory)
 */

class BrowserHistory {
public:
  BrowserHistory(string homepage) { curr = homepage; }
  void visit(string url) {
    h_forward = stack<string>();
    h_back.push(curr);
    curr = url;
  }
  string back(int steps) {
    while (--steps >= 0 && !h_back.empty()) {
      h_forward.push(curr);
      curr = h_back.top();
      h_back.pop();
    }
    return curr;
  }
  string forward(int steps) {
    while (--steps >= 0 && !h_forward.empty()) {
      h_back.push(curr);
      curr = h_forward.top();
      h_forward.pop();
    }
    return curr;
  }

private:
  stack<string> h_back, h_forward;
  string curr;
};

int main() {
  BrowserHistory *obj = new BrowserHistory("https.xyz");
  string param_2 = obj->back(1);
  string param_3 = obj->forward(1);
}