Forward & Backward buttons on a browser using Stacks
A data structure case study.
Team 14:
Surya K(1MS21CY056)
Davin H(1MS21CY012)
Rohan S(1MS21CY044)
Motive of this Case Study
Motive of this Case Study
- To demonstrate the use of stack data structures in web development
- To understand the tracking of history of button clicks
- To ensure proper order of operations
- To provide insights on undo and redo actions
What are Stacks?
A linear data structure following LIFO with various operations and implementations.
What exactly are the forward and backward buttons?
Webpage history stacks using pushing and popping
The Approach
- Initialize currentStateURL that will store the current URL of the Browser.
- Initialize two stacks forwardStack and backwardStack that will store the sequence of URLs accessed when the forward and the backward buttons are pressed respectively.
- While visiting any new URL, push it into backwardStack.
- While pressing the forward button push currentStateURL into backwardStack and pop the last URL from forwardStack and set it as currentStateURL.
- While pressing the backward button, push currentStateURL into forwardStack and pop the last URL from backwardStack, and set it as currentStateURL.
The Code
// Function to handle state when the
// backward button is pressed
void backward()
{
// If current url is the last url
if (backward_stack.empty()
|| current_state_url
== backward_stack.top()) {
cout << "Not Available\n";
return;
}
// Otherwise
else {
// Push current url to the
// forward stack
forward_stack.push(
current_state_url);
// Set current url to top
// of backward stack
current_state_url
= backward_stack.top();
// Pop it from backward stack
backward_stack.pop();
}
}
// Function to handle state when the
// forward button is pressed
void forward(){ // If current url is the last url
if (forward_stack.empty()
|| current_state_url
== forward_stack.top()) {
cout << "Not Available\n";
return;
} else {
// Push current state to the
// backward stack
backward_stack.push(
current_state_url);
// Set current state to top
// of forward stack
current_state_url
= forward_stack.top();
// Remove from forward stack
forward_stack.pop();
}
}
include <bits/stdc++.h>using namespace std;// Stores the current visiting page
string current_state_url = "";
// Stores url when pressed forward
stack<string> forward_stack;
// Stores url when pressed backward
stack<string> backward_stack;
// Function for when visit a url
void visit_new_url(string url)
{
// If current URL is empty
if (current_state_url != "") {
// Push into backward_stack
backward_stack.push(
current_state_url);
}
// Set curr_state_url to url
current_state_url = url;
}
The Code
// Function that performs the process
// of pressing forward and backward
// button in a Browser
void simulatorFunction()
{
// Current URL
string url = "amazon.com";
// Visit the current URL
visit_new_url(url);
// Print the current URL
cout << "Current URL is: "
<< current_state_url
<< " \n";
// New current URL
url = "flipkart.com";
// Visit the current URL
visit_new_url(url);
// Print the current URL
cout << "Current URL is: "
<< current_state_url
<< " \n";
// Driver Code
int main()
{
// Function to simulate process of
// pressing forward & backward button
simulatorFunction();
return 0;
}
// Pressed backward button
backward();
// Print the current URL
cout << "Current URL after pressing"
<< " Backward button is: "
<< current_state_url
<< " \n";
// Pressed forward button
forward();
// Print the current URL
cout << "Current URL after pressing"
<< " Forward button is: "
<< current_state_url
<< " \n";
// New current URL
url = "myntra.com";
// Visit the current URL
visit_new_url(url);
// Print the current URL
cout << "Current URL is: "
<< current_state_url
<< " \n";
forward();
}
Output
Current URL is: amazon.com Current URL is: flipkart.com
Current URL after pressing Backward button is: amazon.com
Current URL after pressing Forward button is: flipkart.com
Current URL is: myntra.com
Not Available
Thank you!
Hope this presentation was insightful!
Stacks in Browsers
Rohan Santhosh
Created on January 15, 2023
Start designing with a free template
Discover more than 1500 professional designs like these:
View
Relaxing Presentation
View
Modern Presentation
View
Colorful Presentation
View
Modular Structure Presentation
View
Chromatic Presentation
View
City Presentation
View
News Presentation
Explore all templates
Transcript
Forward & Backward buttons on a browser using Stacks
A data structure case study.
Team 14:
Surya K(1MS21CY056)
Davin H(1MS21CY012)
Rohan S(1MS21CY044)
Motive of this Case Study
Motive of this Case Study
What are Stacks?
A linear data structure following LIFO with various operations and implementations.
What exactly are the forward and backward buttons?
Webpage history stacks using pushing and popping
The Approach
The Code
// Function to handle state when the // backward button is pressed void backward() { // If current url is the last url if (backward_stack.empty() || current_state_url == backward_stack.top()) { cout << "Not Available\n"; return; } // Otherwise else { // Push current url to the // forward stack forward_stack.push( current_state_url); // Set current url to top // of backward stack current_state_url = backward_stack.top(); // Pop it from backward stack backward_stack.pop(); } }
// Function to handle state when the // forward button is pressed void forward(){ // If current url is the last url if (forward_stack.empty() || current_state_url == forward_stack.top()) { cout << "Not Available\n"; return; } else { // Push current state to the // backward stack backward_stack.push( current_state_url); // Set current state to top // of forward stack current_state_url = forward_stack.top(); // Remove from forward stack forward_stack.pop(); } }
include <bits/stdc++.h>using namespace std;// Stores the current visiting page string current_state_url = ""; // Stores url when pressed forward stack<string> forward_stack; // Stores url when pressed backward stack<string> backward_stack; // Function for when visit a url void visit_new_url(string url) { // If current URL is empty if (current_state_url != "") { // Push into backward_stack backward_stack.push( current_state_url); } // Set curr_state_url to url current_state_url = url; }
The Code
// Function that performs the process // of pressing forward and backward // button in a Browser void simulatorFunction() { // Current URL string url = "amazon.com"; // Visit the current URL visit_new_url(url); // Print the current URL cout << "Current URL is: " << current_state_url << " \n"; // New current URL url = "flipkart.com"; // Visit the current URL visit_new_url(url); // Print the current URL cout << "Current URL is: " << current_state_url << " \n";
// Driver Code int main() { // Function to simulate process of // pressing forward & backward button simulatorFunction(); return 0; }
// Pressed backward button backward(); // Print the current URL cout << "Current URL after pressing" << " Backward button is: " << current_state_url << " \n"; // Pressed forward button forward(); // Print the current URL cout << "Current URL after pressing" << " Forward button is: " << current_state_url << " \n"; // New current URL url = "myntra.com"; // Visit the current URL visit_new_url(url); // Print the current URL cout << "Current URL is: " << current_state_url << " \n"; forward(); }
Output
Current URL is: amazon.com Current URL is: flipkart.com Current URL after pressing Backward button is: amazon.com Current URL after pressing Forward button is: flipkart.com Current URL is: myntra.com Not Available
Thank you!
Hope this presentation was insightful!