1
+ class DLL :
2
+ """
3
+ a doubly linked list that holds the current page,
4
+ next page, and previous page.
5
+ Used to enforce order in operations
6
+ """
7
+ def __init__ (self , val : str = None ):
8
+ self .val = val
9
+ self .nxt = None
10
+ self .prev = None
11
+
12
+
13
+ class BrowserHistory :
14
+ """
15
+ This class designs the operations of a browser history
16
+
17
+ It works by using a doubly linked list to hold the urls
18
+ """
19
+
20
+ def __init__ (self , homepage : str ):
21
+ """
22
+ Returns - None
23
+ Input - None
24
+ ----------
25
+ - Initialize doubly linked list which will serve as the
26
+ browser history and sets the current page
27
+ """
28
+ self .head = DLL (homepage )
29
+ self .curr = self .head
30
+
31
+ def visit (self , url : str ) -> None :
32
+ """
33
+ Returns - None
34
+ Input - str
35
+ ----------
36
+ - Adds the current url to the DLL
37
+ - sets both the next and previous values
38
+ """
39
+ url_node = DLL (url )
40
+ self .curr .nxt = url_node
41
+ url_node .prev = self .curr
42
+
43
+ self .curr = url_node
44
+
45
+
46
+ def back (self , steps : int ) -> str :
47
+ """
48
+ Returns - str
49
+ Input - int
50
+ ----------
51
+ - Iterates through the DLL backwards `step` number of times
52
+ - returns the appropriate value
53
+ """
54
+ while steps > 0 and self .curr .prev :
55
+ self .curr = self .curr .prev
56
+ steps -= 1
57
+ return self .curr .val
58
+
59
+
60
+ def forward (self , steps : int ) -> str :
61
+ """
62
+ Returns - str
63
+ Input - int
64
+ ----------
65
+ - Iterates through the DLL forewards `step` number of times
66
+ - returns the appropriate value
67
+ """
68
+ while steps > 0 and self .curr .nxt :
69
+ self .curr = self .curr .nxt
70
+ steps -= 1
71
+ return self .curr .val
72
+
73
+
74
+ if __name__ == "__main__" :
75
+ obj = BrowserHistory ("google.com" )
76
+ obj .visit ("twitter.com" )
77
+ param_2 = obj .back (1 )
78
+ param_3 = obj .forward (1 )
79
+
80
+ print (param_2 )
81
+ print (param_3 )
0 commit comments