Challenge: Implementing BrowserHistory
Swipe to start coding
In this task, you need to complete the BrowserHistory class by filling in the missing parts. The goal is to implement a basic browser history system using two stacks: one for back navigation (backStack) and one for forward navigation (forwardStack).
You will handle visiting new pages, moving backward and forward through history, and keeping track of the current page. Use Deque.
-
In the
visit(String url)method:- Add the
currentPageto thebackStack. - Update
currentPageto the newurl. - Clear the
forwardStackto reset forward history.
- Add the
-
In the
back(int steps)method:- The loop should continue while
steps > 0andbackStackis not empty. - Push the
currentPageonto theforwardStack. - Pop the last page from the
backStackand assign it tocurrentPage. - After moving backward, return the
currentPage.
- The loop should continue while
-
In the
forward(int steps)method:- The loop should continue while
steps > 0andforwardStackis not empty. - Push the
currentPageonto thebackStack. - Pop the last page from the
forwardStackand assign it tocurrentPage. - After moving forward, return the
currentPage.
- The loop should continue while
-
In the
getCurrentPage()method:- Simply return the
currentPage.
- Simply return the
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 6.25
Challenge: Implementing BrowserHistory
Svep för att visa menyn
Swipe to start coding
In this task, you need to complete the BrowserHistory class by filling in the missing parts. The goal is to implement a basic browser history system using two stacks: one for back navigation (backStack) and one for forward navigation (forwardStack).
You will handle visiting new pages, moving backward and forward through history, and keeping track of the current page. Use Deque.
-
In the
visit(String url)method:- Add the
currentPageto thebackStack. - Update
currentPageto the newurl. - Clear the
forwardStackto reset forward history.
- Add the
-
In the
back(int steps)method:- The loop should continue while
steps > 0andbackStackis not empty. - Push the
currentPageonto theforwardStack. - Pop the last page from the
backStackand assign it tocurrentPage. - After moving backward, return the
currentPage.
- The loop should continue while
-
In the
forward(int steps)method:- The loop should continue while
steps > 0andforwardStackis not empty. - Push the
currentPageonto thebackStack. - Pop the last page from the
forwardStackand assign it tocurrentPage. - After moving forward, return the
currentPage.
- The loop should continue while
-
In the
getCurrentPage()method:- Simply return the
currentPage.
- Simply return the
Lösning
Tack för dina kommentarer!
single