Implementación de los botones Atrás y Adelante del navegador

Diseñe los botones de avance y retroceso de un navegador utilizando la estructura de datos de pila . Si en algún momento, la URL no existe después de presionar cualquiera de los dos botones, imprima «No disponible» . De lo contrario, imprima la URL actual .

Enfoque: la idea es usar dos pilas hacia adelante y hacia atrás para realizar un seguimiento de las URL visitadas y una variable » currentStateURL » para almacenar la URL visitada actualmente. Siga los pasos a continuación para resolver el problema:

  • Inicialice currentStateURL que almacenará la URL actual del navegador.
  • Inicialice dos pilas forwardStack ybackStack que almacenarán la secuencia de URL a las que se accede cuando se presionan los botones de avance y retroceso respectivamente.
  • Mientras visita cualquier URL nueva , insértela enbackStack .
  • Mientras presiona el botón de avance, presione currentStateURL enbackStack y extraiga la última URL de forwardStack y configúrela como currentStateURL.
  • Mientras presiona el botón de retroceso, presione currentStateURL en forwardStack y extraiga la última URL deBACKStack , y configúrela como currentStateURL .

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
 
#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;
}
 
// 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;
    }
 
    // Otherwise
    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();
    }
}
 
// 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 that performs the process
// of pressing forward and backward
// button in a Browser
void simulatorFunction()
{
    // Current URL
    string url = "ajay.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 = "abc.com";
 
    // Visit the current URL
    visit_new_url(url);
 
    // Print the current URL
    cout << "Current URL is: "
         << current_state_url
         << " \n";
 
    // 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 = "nikhil.com";
 
    // Visit the current URL
    visit_new_url(url);
 
    // Print the current URL
    cout << "Current URL 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";
    // Pressed backward button
    backward();
 
    // Print the current URL
    cout << "Current URL after pressing"
         << " Backward button is: "
         << current_state_url
         << " \n";
}
 
// Driver Code
int main()
{
    // Function to simulate process of
    // pressing forward & backward button
    simulatorFunction();
}

Java

// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Stores the current
// visiting page
static String current_state_url = "";
 
// Stores url when pressed forward
static Stack<String>
       forward_stack = new Stack<>();
 
// Stores url when pressed backward
static Stack<String>
       backward_stack = new Stack<>();
 
// Function for when visit a url
static void visit_new_url(String url)
{
  // If current URL is empty
  if (current_state_url != "")
  {
    // Push into backward_stack
    backward_stack.add(
             current_state_url);
  }
 
  // Set curr_state_url to url
  current_state_url = url;
}
 
// Function to handle state
// when the forward button
// is pressed
static void forward()
{
  // If current url is the last url
  if (forward_stack.isEmpty() ||
      current_state_url ==
      forward_stack.peek())
  {
    System.out.print("Not Available\n");
    return;
  }
 
  // Otherwise
  else
  {
    // Push current state to the
    // backward stack
    backward_stack.add(
             current_state_url);
 
    // Set current state to top
    // of forward stack
    current_state_url =
            forward_stack.peek();
 
    // Remove from forward
    // stack
    forward_stack.pop();
  }
}
 
// Function to handle state
// when the backward button
// is pressed
static void backward()
{
  // If current url is the
  // last url
  if (backward_stack.isEmpty() ||
      current_state_url ==
      backward_stack.peek())
  {
    System.out.print("Not Available\n");
    return;
  }
 
  // Otherwise
  else
  {
    // Push current url to the
    // forward stack
    forward_stack.add(
            current_state_url);
 
    // Set current url to top
    // of backward stack
    current_state_url =
            backward_stack.peek();
 
    // Pop it from backward
    // stack
    backward_stack.pop();
  }
}
 
// Function that performs the
// process of pressing forward
// and backward button in a
// Browser
static void simulatorFunction()
{
  // Current URL
  String url = "ajay.com";
 
  // Visit the current URL
  visit_new_url(url);
 
  // Print the current URL
  System.out.print("Current URL is: " +
                   current_state_url +
                   " \n");
 
  // New current URL
  url = "abc.com";
 
  // Visit the current URL
  visit_new_url(url);
 
  // Print the current URL
  System.out.print("Current URL is: " +
                   current_state_url +
                   " \n");
 
  // Pressed backward button
  backward();
 
  // Print the current URL
  System.out.print("Current URL after pressing" +
                   " Backward button is: " +
                   current_state_url + " \n");
 
  // Pressed forward button
  forward();
 
  // Print the current URL
  System.out.print("Current URL after pressing" +
                   " Forward button is: " +
                   current_state_url + " \n");
 
    // New current URL
    url = "nikhil.com";
 
    // Visit the current URL
    visit_new_url(url);
 
    // Print the current URL
    System.out.print("Current URL is: " +
                     current_state_url +
                     " \n");
 
    // Pressed forward button
    forward();
 
    // Print the current URL
    System.out.print("Current URL after pressing" +
                     " Forward button is: " +
                     current_state_url + " \n");
    // Pressed backward button
    backward();
 
    // Print the current URL
    System.out.print("Current URL after pressing" +
                     " Backward button is: " +
                     current_state_url + " \n");
}
 
// Driver Code
public static void main(String[] args)
{
  // Function to simulate process of
  // pressing forward & backward button
  simulatorFunction();
}
}
 
// This code is contributed by shikhasingrajput

Python3

# Python3 program for the above approach
 
# Stores the current
# visiting page
current_state_url = ""
  
# Stores url when pressed forward
forward_stack = []
  
# Stores url when pressed backward
backward_stack = []   
  
# Function for when visit a url
def visit_new_url(url):
  global current_state_url
   
  # If current URL is empty
  if (current_state_url != ""):
    # Push into backward_stack
    backward_stack.append(current_state_url)
  
  # Set curr_state_url to url
  current_state_url = url
  
# Function to handle state
# when the forward button
# is pressed
def forward():
  # If current url is the last url
  if (len(forward_stack) == 0 or current_state_url == forward_stack[-1]):
    print("Not Available")
    return
  
  # Otherwise
  else:
    # Push current state to the
    # backward stack
    backward_stack.append(current_state_url)
  
    # Set current state to top
    # of forward stack
    current_state_url = forward_stack[-1]
  
    # Remove from forward
    # stack
    forward_stack.pop()
  
# Function to handle state
# when the backward button
# is pressed
def backward():
  # If current url is the
  # last url
  if (len(backward_stack) != 0 or current_state_url == backward_stack[-1]):
    print("Not Available")
    return
  
  # Otherwise
  else:
    # Push current url to the
    # forward stack
    forward_stack.append(current_state_url)
  
    # Set current url to top
    # of backward stack
    current_state_url = backward_stack[-1]
  
    # Pop it from backward
    # stack
    backward_stack[-1]
  
# Function that performs the
# process of pressing forward
# and backward button in a
# Browser
def simulatorFunction():
  # Current URL
  url = "ajay.com"
  
  # Visit the current URL
  visit_new_url(url)
  
  # Print the current URL
  print("Current URL is: " + current_state_url)
  
  # New current URL
  url = "abc.com"
  
  # Visit the current URL
  visit_new_url(url)
  
  # Print the current URL
  print("Current URL is: " + current_state_url)
  
  # Pressed backward button
  backward()
  
  # Print the current URL
  print("Current URL after pressing" + " Backward button is: " + current_state_url)
  
  # Pressed forward button
  forward()
  
  # Print the current URL
  print("Current URL after pressing" + " Forward button is: " + current_state_url)
  
  # New current URL
  url = "nikhil.com"
  
  # Visit the current URL
  visit_new_url(url)
  
  # Print the current URL
  print("Current URL is: " + current_state_url)
  
  # Pressed forward button
  forward()
  
  # Print the current URL
  print("Current URL after pressing" + " Forward button is: " + current_state_url)
  # Pressed backward button
  backward()
  
  # Print the current URL
  print("Current URL after pressing" + " Backward button is: " + current_state_url)
   
# Function to simulate process of
# pressing forward & backward button
simulatorFunction()
 
# This code is contributed by suresh07.

C#

// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Stores the current
// visiting page
static String current_state_url = "";
 
// Stores url when pressed forward
static Stack<String>
       forward_stack = new Stack<String>();
 
// Stores url when pressed backward
static Stack<String>
       backward_stack = new Stack<String>();     
 
// Function for when visit a url
static 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;
}
 
// Function to handle state
// when the forward button
// is pressed
static void forward()
{
  // If current url is the last url
  if (forward_stack.Count == 0 ||
      current_state_url ==
      forward_stack.Peek())
  {
    Console.Write("Not Available\n");
    return;
  }
 
  // Otherwise
  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.Peek();
 
    // Remove from forward
    // stack
    forward_stack.Pop();
  }
}
 
// Function to handle state
// when the backward button
// is pressed
static void backward()
{
  // If current url is the
  // last url
  if (backward_stack.Count != 0 ||
      current_state_url ==
      backward_stack.Peek())
  {
    Console.Write("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.Peek();
 
    // Pop it from backward
    // stack
    backward_stack.Pop();
  }
}
 
// Function that performs the
// process of pressing forward
// and backward button in a
// Browser
static void simulatorFunction()
{
  // Current URL
  String url = "ajay.com";
 
  // Visit the current URL
  visit_new_url(url);
 
  // Print the current URL
  Console.Write("Current URL is: " +
                current_state_url +
                " \n");
 
  // New current URL
  url = "abc.com";
 
  // Visit the current URL
  visit_new_url(url);
 
  // Print the current URL
  Console.Write("Current URL is: " +
                current_state_url +
                " \n");
 
  // Pressed backward button
  backward();
 
  // Print the current URL
  Console.Write("Current URL after pressing" +
                " Backward button is: " +
                current_state_url + " \n");
 
  // Pressed forward button
  forward();
 
  // Print the current URL
  Console.Write("Current URL after pressing" +
                " Forward button is: " +
                current_state_url + " \n");
 
  // New current URL
  url = "nikhil.com";
 
  // Visit the current URL
  visit_new_url(url);
 
  // Print the current URL
  Console.Write("Current URL is: " +
                current_state_url +
                " \n");
 
  // Pressed forward button
  forward();
 
  // Print the current URL
  Console.Write("Current URL after pressing" +
                " Forward button is: " +
                current_state_url + " \n");
  // Pressed backward button
  backward();
 
  // Print the current URL
  Console.Write("Current URL after pressing" +
                " Backward button is: " +
                current_state_url + " \n");
}
 
// Driver Code
public static void Main(String[] args)
{
  // Function to simulate process of
  // pressing forward & backward button
  simulatorFunction();
}
}
 
// This code is contributed by shikhasingrajput

Javascript

<script>
    // Javascript program for the above approach
     
    // Stores the current
    // visiting page
    let current_state_url = "";
 
    // Stores url when pressed forward
    let forward_stack = [];
 
    // Stores url when pressed backward
    let backward_stack = []; 
 
    // Function for when visit a url
    function visit_new_url(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;
    }
 
    // Function to handle state
    // when the forward button
    // is pressed
    function forward()
    {
      // If current url is the last url
      if (forward_stack.length == 0 ||
          current_state_url == forward_stack[forward_stack.length - 1])
      {
        document.write("Not Available" + "</br>");
        return;
      }
 
      // Otherwise
      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[forward_stack.length - 1];
 
        // Remove from forward
        // stack
        forward_stack.pop();
      }
    }
 
    // Function to handle state
    // when the backward button
    // is pressed
    function backward()
    {
      // If current url is the
      // last url
      if (backward_stack.length != 0 ||
          current_state_url ==
          backward_stack[backward_stack.length - 1])
      {
        document.write("Not Available" + "</br>");
        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[backward_stack.length - 1];
 
        // Pop it from backward
        // stack
        backward_stack.pop();
      }
    }
 
    // Function that performs the
    // process of pressing forward
    // and backward button in a
    // Browser
    function simulatorFunction()
    {
      // Current URL
      let url = "ajay.com";
 
      // Visit the current URL
      visit_new_url(url);
 
      // Print the current URL
      document.write("Current URL is: " + current_state_url + "</br>");
 
      // New current URL
      url = "abc.com";
 
      // Visit the current URL
      visit_new_url(url);
 
      // Print the current URL
      document.write("Current URL is: " + current_state_url + "</br>");
 
      // Pressed backward button
      backward();
 
      // Print the current URL
      document.write("Current URL after pressing" + " Backward button is: " +
                    current_state_url + " </br>");
 
      // Pressed forward button
      forward();
 
      // Print the current URL
      document.write("Current URL after pressing" +
                    " Forward button is: " +
                    current_state_url + " </br>");
 
      // New current URL
      url = "nikhil.com";
 
      // Visit the current URL
      visit_new_url(url);
 
      // Print the current URL
      document.write("Current URL is: " +
                    current_state_url +
                    " </br>");
 
      // Pressed forward button
      forward();
 
      // Print the current URL
      document.write("Current URL after pressing" +
                    " Forward button is: " +
                    current_state_url + " </br>");
      // Pressed backward button
      backward();
 
      // Print the current URL
      document.write("Current URL after pressing" +
                    " Backward button is: " +
                    current_state_url + " </br>");
    }
     
    // Function to simulate process of
    // pressing forward & backward button
    simulatorFunction();
 
// This code is contributed by rameshtravel07.
</script>
Producción

Current URL is: ajay.com 
Current URL is: abc.com 
Current URL after pressing Backward button is: ajay.com 
Current URL after pressing Forward button is: abc.com 
Current URL is: nikhil.com 
Not Available
Current URL after pressing Forward button is: nikhil.com 
Current URL after pressing Backward button is: abc.com 

 

Complejidad temporal: O(N). 
Espacio Auxiliar: O(N).

Publicación traducida automáticamente

Artículo escrito por ajaykr00kj y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *