
1- Browser(homepage): Set homepage of the browser
2- Visit(url): Visit the url from the current page. It clears up all the forward history.
3- Back(steps): Move ‘steps’ backward in the browser history
4- Forward(steps): Move ‘steps’ forward in the browser history
If you can’t move steps forward or backward, just return the last website that can be reached.
The Queries are in the given format-:
The first line of each query contains the string representing the homepage of the web browser.
(1, url): Visit the url from the current page.
(2, steps): Move ‘steps’ backward in the browser history.
(3, steps): Move ‘steps’ forward in the browser history.
You are queries as [[“homepage.com”], [1 , “facebook.com”], [1, “codingninjas.com”],[2, 1], [3, 1]]
1 query is the query that sets the homepage as “homepage.com”.
2 query is the query to visit “facebook.com”
3 query is the query to visit “codingninjas.com”
4 query is the query that moves back one step to “facebook.com”
5 query is the query that moves forward one step to “codingninjas.com”
Hence the answer is [“facebook.com”, “codingninjas.com”]
The first line of input contains a single integer ‘Q’, representing the number of queries.
The next line contains the homepage.
The next ‘Q-1’ lines contain space-separated strings and integers denoting the queries.
The output is a space-separated strings representing the output of the queries.
You do not need to print anything. It has already been taken care of. Just implement the function.
In this approach, we will maintain a doubly linked list and keep track of the current node the linked list, each time we visit a website we will remove every node in front of current node and the new url as the current node in the linked list
We will create a Node(url) class where url is the URL of the given node, there are prev and next as previous and next node pointers respectively.
Algorithm:
In this approach, we maintain two stacks, one to store the history and another one every time we go back. We store the previous websites to it. We will reset the second stack each time we visit a website and only fill the stack if we do a back operation.
Algorithm:
In this approach, we will maintain the single array of all the sites visited, every time we visit a new site, we remove all the sites in front of it. If we go back more than history has, then we simply return the last element of the list
Algorithm: