Want to create interactive content? It’s easy in Genially!

Get started free

Stacks in Browsers

Rohan Santhosh

Created on January 15, 2023

Start designing with a free template

Discover more than 1500 professional designs like these:

Relaxing Presentation

Modern Presentation

Colorful Presentation

Modular Structure Presentation

Chromatic Presentation

City Presentation

News Presentation

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

  • 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

  1. Initialize currentStateURL that will store the current URL of the Browser.
  2. Initialize two stacks forwardStack and backwardStack that will store the sequence of URLs accessed when the forward and the backward buttons are pressed respectively.
  3. While visiting any new URL, push it into backwardStack.
  4. While pressing the forward button push currentStateURL into backwardStack and pop the last URL from forwardStack and set it as currentStateURL.
  5. 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!