@@ -3,7 +3,6 @@ class DLL:
33 a doubly linked list that holds the current page,
44 next page, and previous page.
55 Used to enforce order in operations.
6- This is a change to the file
76 """
87 def __init__ (self , val : str = None ):
98 self .val = val
@@ -15,61 +14,82 @@ class BrowserHistory:
1514 """
1615 This class designs the operations of a browser history
1716
18- It works by using a doubly linked list to hold the urls
17+ It works by using a doubly linked list to hold the urls with optimized
18+ navigation using step counters and memory management
1919 """
2020
2121 def __init__ (self , homepage : str ):
2222 """
2323 Returns - None
24- Input - None
24+ Input - str
2525 ----------
2626 - Initialize doubly linked list which will serve as the
2727 browser history and sets the current page
28+ - Initialize navigation counters
2829 """
29- self .head = DLL (homepage )
30- self .curr = self .head
30+ self ._head = DLL (homepage )
31+ self ._curr = self ._head
32+ self ._back_count = 0
33+ self ._forward_count = 0
3134
3235 def visit (self , url : str ) -> None :
3336 """
3437 Returns - None
3538 Input - str
3639 ----------
3740 - Adds the current url to the DLL
38- - sets both the next and previous values
41+ - Sets both the next and previous values
42+ - Cleans up forward history to prevent memory leaks
43+ - Resets forward count and increments back count
3944 """
40- url_node = DLL ( url )
41- self .curr .nxt = url_node
42- url_node . prev = self . curr
45+ # Clear forward history to prevent memory leaks
46+ self ._curr .nxt = None
47+ self . _forward_count = 0
4348
44- self .curr = url_node
49+ # Create and link new node
50+ url_node = DLL (url )
51+ self ._curr .nxt = url_node
52+ url_node .prev = self ._curr
4553
54+ # Update current node and counts
55+ self ._curr = url_node
56+ self ._back_count += 1
4657
4758 def back (self , steps : int ) -> str :
4859 """
4960 Returns - str
5061 Input - int
5162 ----------
52- - Iterates through the DLL backwards `step` number of times
53- - returns the appropriate value
63+ - Moves backwards through history up to available steps
64+ - Updates navigation counters
65+ - Returns current page URL
5466 """
55- while steps > 0 and self .curr .prev :
56- self .curr = self .curr .prev
67+ # Only traverse available nodes
68+ steps = min (steps , self ._back_count )
69+ while steps > 0 :
70+ self ._curr = self ._curr .prev
5771 steps -= 1
58- return self .curr .val
59-
72+ self ._back_count -= 1
73+ self ._forward_count += 1
74+ return self ._curr .val
6075
6176 def forward (self , steps : int ) -> str :
6277 """
6378 Returns - str
6479 Input - int
6580 ----------
66- - Iterates through the DLL forewards `step` number of times
67- - returns the appropriate value
81+ - Moves forward through history up to available steps
82+ - Updates navigation counters
83+ - Returns current page URL
6884 """
69- while steps > 0 and self .curr .nxt :
70- self .curr = self .curr .nxt
85+ # Only traverse available nodes
86+ steps = min (steps , self ._forward_count )
87+ while steps > 0 :
88+ self ._curr = self ._curr .nxt
7189 steps -= 1
72- return self .curr .val
90+ self ._forward_count -= 1
91+ self ._back_count += 1
92+ return self ._curr .val
7393
7494
7595if __name__ == "__main__" :
0 commit comments