Stack for browser history
Browsers let you move Back to the page you came from and sometimes Forward again. That behavior lines up with two stacks plus a current page: one stack stores older pages you can return to, the other stores pages you stepped back through so you can replay them forward. When you open a new link, the forward branch is usually discarded—just like clearing redo after a new edit in an editor.
- Back stack — pages behind you (LIFO)
- Forward stack — pages you can return to after Back
- Rules — visit, Back, Forward
- Example in C — string URLs in arrays
1) Two stacks and a current page
| Piece | Meaning |
|---|---|
| Current page | The page you are viewing now. It is not inside either stack in this teaching model. |
| Back stack | Pages you can go back to. The top is the page you visited immediately before the current one. |
| Forward stack | Pages you left by pressing Back. The top is the next page Forward will restore. |
Snapshot after visiting /, then /topics, then /data-structures:
/data-structuresBack stack
Bottom → top (next Back restores /topics)
Pressing Back moves /data-structures onto the forward stack and restores /topics.
Forward stack
No Back presses yet
Forward stays empty until you use Back at least once.
Real browsers add session restore, bfcache, redirects, and SPA routing (history.pushState). The two-stack picture still explains the LIFO slice of Back and Forward.
2) Rules
| Event | Effect |
|---|---|
| Visit a new URL | If there is a current page, push it onto the back stack. Clear the forward stack. Set current to the new URL. |
| Back | If the back stack is not empty: push current onto forward, then pop the back stack into current. |
| Forward | If the forward stack is not empty: push current onto back, then pop the forward stack into current. |
3) Example in C
The program visits three paths, prints the current URL, goes Back twice, then Forward once. Arrays act as both stacks; current holds the active page. strcpy keeps the example short—check lengths and bounds in real code.
#include <stdio.h>
#include <string.h>
#define MAX 8
#define URL_LEN 48
char back_stack[MAX][URL_LEN];
char forward_stack[MAX][URL_LEN];
int back_top = -1;
int forward_top = -1;
char current[URL_LEN] = "";
// New visit: remember current, discard forward branch
void visit(const char *url) {
if (current[0] != '\0')
strcpy(back_stack[++back_top], current);
forward_top = -1;
strcpy(current, url);
}
// Back: stash current on forward; restore previous
void go_back(void) {
if (back_top < 0)
return;
strcpy(forward_stack[++forward_top], current);
strcpy(current, back_stack[back_top--]);
}
// Forward: stash current on back; replay next
void go_forward(void) {
if (forward_top < 0)
return;
strcpy(back_stack[++back_top], current);
strcpy(current, forward_stack[forward_top--]);
}
int main(void) {
visit("/");
visit("/topics");
visit("/data-structures");
printf("current: %s\n", current);
go_back();
printf("current: %s\n", current);
go_back();
printf("current: %s\n", current);
go_forward();
printf("current: %s\n", current);
return 0;
}
Expected lines: current: /data-structures, /topics, /, then /topics again.
Trace (abbreviated)
Stacks list entries bottom → top. The top of forward is the next Forward destination.
| Step | Action | Current | Back (bottom → top) | Forward (bottom → top) |
|---|---|---|---|---|
| 1–3 | Visit /, /topics, /data-structures |
/data-structures |
/ … /topics |
(empty) |
| 4 | Back | /topics |
/ |
/data-structures |
| 5 | Back | / |
(empty) |
/data-structures … /topics (top /topics) |
| 6 | Forward | /topics |
/ |
/data-structures |
4) Trade-offs
- Pros — Matches how users think about Back/Forward; easy to teach with stacks.
- Cons — Real engines merge entries, prune redirects, and handle in-page history; SPAs may virtualize this with a router state machine.
Close cousin: Stack for undo / redo · Stack applications.
Key takeaway
Back is a stack of pages behind you. Forward is a second stack of pages you stepped back through. A new visit clears forward so the branch matches where you actually went next.
See also: Stack for undo / redo · Stack for string reverse · Stack tutorial