Advertisement
exmkg

Untitled

Sep 23rd, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. class BrowserHistory {
  2.     public class Node {
  3.         String url;
  4.         Node prev, next;
  5.        
  6.         public Node(String url) {
  7.             this.url = url;
  8.             this.next = null;
  9.             this.prev = null;
  10.         }
  11.     }
  12.    
  13.     private Node current;
  14.  
  15.     public BrowserHistory(String homepage) {
  16.         current = new Node(homepage);
  17.     }
  18.    
  19.     public void visit(String url) {
  20.         Node newUrl = new Node(url);
  21.         current.next = newUrl;
  22.         newUrl.prev = current;
  23.         current = newUrl;
  24.     }
  25.    
  26.     public String back(int steps) {
  27.         for (; current.prev != null && steps > 0; steps--) {
  28.             current = current.prev;
  29.         }
  30.         return current.url;
  31.     }
  32.    
  33.     public String forward(int steps) {
  34.         for (; current.next != null && steps > 0; steps--) {
  35.             current = current.next;
  36.         }
  37.         return current.url;
  38.     }
  39. }
  40.  
  41. /**
  42.  * Your BrowserHistory object will be instantiated and called as such:
  43.  * BrowserHistory obj = new BrowserHistory(homepage);
  44.  * obj.visit(url);
  45.  * String param_2 = obj.back(steps);
  46.  * String param_3 = obj.forward(steps);
  47.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement