If you were asked to design the backward and forward button operation (as a library) in a browser, how would you do it? What will be your APIs and parameters to them? what data structure will be best suitable for this usecase?
3
Simplest solution would be to use two stacks (back, forward).
Clicking the back button will pop from the back stack and push the current page on the forward stack (and go to the popped value on the back stack).
Clicking on any link on a page (following a link) will clear the forward stack and push the current page on the back stack.
Clicking on the forward button will pop from the forward stack and push the current page on the back stack (and go to the popped value on the forward stack).
The back and forward buttons are disabled when the appropriate stack is empty.
4
I would use an array containing urls, with a variable storing the length and an index pointing to the current item being displayed.
Back decrements the index, Forward increments the index. New page increments the index, stores the url at that place in the array and sets the length to the index value.
When the array is full, I would either use a realloc()
for requesting more memory or implement a ring access to the array, depending on the memory constraints.
6