Exercise: Browser History Navigation

Implement a class-based browser navigation system using stacks to manage visiting, returning to, and going forward to web pages.

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 Browser class 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 Count property 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 you Push will be the first one you Pop.

  • Remember that visiting a brand new page always invalidates the ability to go “Forward”. You can use the Clear() method on your forward stack when Visit() is called.

  • Before calling Pop(), always wrap your logic in an if (_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 CurrentPage variable with the popped value.

Exercise: Browser History Navigation

Implement a class-based browser navigation system using stacks to manage visiting, returning to, and going forward to web pages.

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 Browser class 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 Count property 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 you Push will be the first one you Pop.

  • Remember that visiting a brand new page always invalidates the ability to go “Forward”. You can use the Clear() method on your forward stack when Visit() is called.

  • Before calling Pop(), always wrap your logic in an if (_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 CurrentPage variable with the popped value.

C# 14.0
namespace Navigation;
using System.Collections.Generic;
public class Browser
{
// 1. Initialize two stacks for back and forward history
public string CurrentPage { get; private set; } = "Blank Page";
public void Visit(string url)
{
// 2. Implement Visit logic
// - Push CurrentPage to back stack
// - Update CurrentPage
// - Clear forward stack
}
public void Back()
{
// 3. Implement Back logic
// - Check if back stack has items
// - Push CurrentPage to forward stack
// - Pop from back stack and set as CurrentPage
}
public void Forward()
{
// 4. Implement Forward logic
// - Check if forward stack has items
// - Push CurrentPage to back stack
// - Pop from forward stack and set as CurrentPage
}
}