📜  实现浏览器的后退和前进按钮

📅  最后修改于: 2021-09-06 06:02:23             🧑  作者: Mango

使用堆栈数据结构设计浏览器的前进和后退按钮。如果在任何情况下,按下两个按钮中的任何一个后 URL 都不存在,则打印“不可用” 。否则,打印当前 URL

方法:我们的想法是使用两个栈向后向前来跟踪访问的URL S和变量“currentStateURL”存储当前访问的URL。请按照以下步骤解决问题:

  • 初始化将存储浏览器当前 URL 的currentStateURL
  • 初始化两个栈forwardStackbackwardStack ,它们将存储分别按下向前和向后按钮时访问的 URL 序列。
  • 在访问任何新 URL 时,将其推入reverseStack
  • 在按下前进按钮的同时将currentStateURL推入backwardStack forwardStack弹出最后一个 URL并将其设置为currentStateURL。
  • 在按下后退按钮的同时,将currentStateURL推入forwardStack并从backwardStack弹出最后一个 URL ,并将其设置为currentStateURL

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include 
using namespace std;
  
// Stores the current visiting page
string current_state_url = "";
  
// Stores url when pressed forward
stack forward_stack;
  
// Stores url when pressed backward
stack 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 steck
        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 
       forward_stack = new Stack<>();
  
// Stores url when pressed backward
static Stack 
       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 steck
    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


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 
       forward_stack = new Stack();
  
// Stores url when pressed backward
static Stack 
       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.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 steck
    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


输出
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 






如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live