1
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
+ """
2
7
def __init__ (self , val : str = None ):
3
8
self .val = val
4
9
self .nxt = None
@@ -7,15 +12,30 @@ def __init__(self, val: str =None):
7
12
8
13
class BrowserHistory :
9
14
"""
10
- This class designs the operations of a
11
- broswer history
15
+ This class designs the operations of a browser history
16
+
17
+ It works by using a doubly linked list to hold the urls
12
18
"""
13
19
14
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
+ """
15
28
self .head = DLL (homepage )
16
29
self .curr = self .head
17
30
18
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
+ """
19
39
url_node = DLL (url )
20
40
self .curr .nxt = url_node
21
41
url_node .prev = self .curr
@@ -24,13 +44,27 @@ def visit(self, url: str) -> None:
24
44
25
45
26
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
+ """
27
54
while steps > 0 and self .curr .prev :
28
55
self .curr = self .curr .prev
29
56
steps -= 1
30
57
return self .curr .val
31
58
32
59
33
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
+ """
34
68
while steps > 0 and self .curr .nxt :
35
69
self .curr = self .curr .nxt
36
70
steps -= 1
0 commit comments