
Sponsored
Sponsored
This approach involves using a list (or similar data structure) to keep track of the pages visited. We use an index to keep track of the current page. Visiting a new page from the current page will truncate the list beyond the current index before adding the new page. Moving back or forward adjusts the index within the bounds of the list.
Time Complexity: Each operation (visit, back, forward) is O(1) on average due to direct index manipulation or list slicing.
Space Complexity: O(n), where n is the number of URLs stored in history.
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4
5typedef struct {
6 char **history;
7 int size;
8 int current;
9 int capacity;
10} BrowserHistory;
11
12BrowserHistory* browserHistoryCreate(char *homepage) {
13 BrowserHistory *obj = (BrowserHistory *)malloc(sizeof(BrowserHistory));
14 obj->history = (char **)malloc(5000 * sizeof(char*));
15 obj->history[0] = strdup(homepage);
16 obj->capacity = 5000;
17 obj->size = 1;
18 obj->current = 0;
19 return obj;
20}
21
22void browserHistoryVisit(BrowserHistory* obj, char *url) {
23 while (obj->size > obj->current + 1) {
24 free(obj->history[--obj->size]);
25 }
26 obj->history[obj->size++] = strdup(url);
27 obj->current = obj->size - 1;
28}
29
30char *browserHistoryBack(BrowserHistory* obj, int steps) {
31 obj->current = (steps > obj->current) ? 0 : obj->current - steps;
32 return obj->history[obj->current];
33}
34
35char *browserHistoryForward(BrowserHistory* obj, int steps) {
36 obj->current = (steps + obj->current >= obj->size) ? obj->size - 1 : obj->current + steps;
37 return obj->history[obj->current];
38}
39
40void browserHistoryFree(BrowserHistory* obj) {
41 for (int i = 0; i < obj->size; i++) {
42 free(obj->history[i]);
43 }
44 free(obj->history);
45 free(obj);
46}The C implementation uses a pre-allocated array of strings (with a max size of 5000 to match constraints) and manually handles all memory allocations and deallocations. 'current' index keeps track of the active site, and functions ‘visit’, ‘back’, and ‘forward’ manipulate it as needed, modifying the array accordingly.
This approach uses two stacks: one to store backward paths and another to store forward paths. The current page is not stored in the stack, but rather observed as the topmost element of the backward stack. The back function pops elements from the backward stack to the forward stack as necessary, and the forward function performs the opposite action. This effectively mimicks the process of moving through the history as we go back and forth.
Time Complexity: Each call to visit, back, or forward is O(steps) due to operations over respective stacks.
Space Complexity: O(n) for maintaining stacks with n pages.
In this Python solution, we use back_stack and forward_stack to keep track of visited pages. 'visit': Pushes the current page to back_stack and resets forward_stack. 'back': Pops pages from back_stack to forward_stack up to steps. 'forward': Pops pages from forward_stack back to back_stack up to steps.