Two Approaches and Code | C++ | 2 Stacks | Doubly Linked List
There are Two Approaches\n1) Using Two Stacks\n\tCreate two stacks history and future\n\nclass BrowserHistory {\npublic:\n    stack<string> history;\n    stack<
# There are Two Approaches\n1) **Using Two Stacks**\n\tCreate two stacks history and future\n```\nclass BrowserHistory {\npublic:\n    stack<string> history;\n    stack<string> future;\n    \n    BrowserHistory(string homepage) {\n        history.push(homepage);\n        future = stack<string>(); //resets stack\n    }\n    \n    void visit(string url) {\n        history.push(url);\n        future = stack<string>();\n    }\n    \n    string back(int steps) {\n        while(steps>0 && history.size() > 1){\n            future.push(history.top());\n            history.pop();\n            steps--;\n        }\n        return history.top();\n    }\n    \n    string forward(int steps) {\n        while(steps>0 && future.size()>0){\n            history.push(future.top());\n            future.pop();\n            steps--;\n        }\n        return history.top();\n    }\n};\n```\n**2) Using Doubly Linked List as a data structure**\n```\nclass BrowserHistory {\npublic:\n    class Node{\n        public:\n        string url;\n        Node* next;\n        Node* prev;\n        \n        Node(string url){\n            this->url =url;\n            next = NULL;\n            prev = NULL;\n        }\n    };\n    Node* curr;\n    BrowserHistory(string homepage) {\n        curr = new Node(homepage);\n    }\n    \n    void visit(string url) {\n        Node* temp = new Node(url);\n        curr->next = temp;\n        temp->prev = curr;\n        curr = curr->next;\n    }\n    \n    string back(int steps) {\n        while(curr->prev!=nullptr && steps-- >0){\n            curr = curr->prev;\n        }\n        return curr->url;\n    }\n    \n    string forward(int steps) {\n        while(curr->next!=nullptr && steps-- >0){\n            curr = curr->next;\n        }\n        return curr->url;\n    }\n};\n```\n*Pls upvote if you like the post!!! *
['Linked List', 'Stack', 'C', 'C++']