Exercise: Browser History Navigation
Problem statement
Real web browsers use Last-In-First-Out (LIFO) data structures to handle navigation. When you visit a new page, the previous page is saved to your history. When you click “Back”, the current page is saved to a “Forward” list, and the previous page is loaded. You will build a complete utility class that encapsulates this exact logic.
Task requirements
Create a
Browserclass inside a defined namespace.Maintain the
CurrentPage, a back history, and a forward history within the class.Implement a
Visit(string url)method that saves the current page to the back history, updates the current page, and clears the forward history.Implement a
Back()method that saves the current page to the forward history, retrieves the most recent page from the back history, and sets it as the current page.Implement a
Forward()method that saves the current page to the back history, retrieves the most recent page from the forward history, and sets it as the current page.
Constraints
You must use two generic
Stack<string>collections: one for the back history and one for the forward history.Use the
Push()method to add pages to the stacks.Use the
Pop()method to retrieve and remove pages from the stacks.Use the
Countproperty to check if a stack has items before attempting to pop.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Remember that a
Stack<T>operates on a Last-In-First-Out (LIFO) principle, meaning the last item youPushwill be the first one youPop.Remember that visiting a brand new page always invalidates the ability to go “Forward”. You can use the
Clear()method on your forward stack whenVisit()is called.Before calling
Pop(), always wrap your logic in anif (_myStack.Count > 0)check to prevent runtime errors when the user clicks back or forward too many times.When moving backward, the current page must be pushed to the forward stack before you overwrite the
CurrentPagevariable with the popped value.
Exercise: Browser History Navigation
Problem statement
Real web browsers use Last-In-First-Out (LIFO) data structures to handle navigation. When you visit a new page, the previous page is saved to your history. When you click “Back”, the current page is saved to a “Forward” list, and the previous page is loaded. You will build a complete utility class that encapsulates this exact logic.
Task requirements
Create a
Browserclass inside a defined namespace.Maintain the
CurrentPage, a back history, and a forward history within the class.Implement a
Visit(string url)method that saves the current page to the back history, updates the current page, and clears the forward history.Implement a
Back()method that saves the current page to the forward history, retrieves the most recent page from the back history, and sets it as the current page.Implement a
Forward()method that saves the current page to the back history, retrieves the most recent page from the forward history, and sets it as the current page.
Constraints
You must use two generic
Stack<string>collections: one for the back history and one for the forward history.Use the
Push()method to add pages to the stacks.Use the
Pop()method to retrieve and remove pages from the stacks.Use the
Countproperty to check if a stack has items before attempting to pop.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Remember that a
Stack<T>operates on a Last-In-First-Out (LIFO) principle, meaning the last item youPushwill be the first one youPop.Remember that visiting a brand new page always invalidates the ability to go “Forward”. You can use the
Clear()method on your forward stack whenVisit()is called.Before calling
Pop(), always wrap your logic in anif (_myStack.Count > 0)check to prevent runtime errors when the user clicks back or forward too many times.When moving backward, the current page must be pushed to the forward stack before you overwrite the
CurrentPagevariable with the popped value.