用栈实现浏览器的前进后退功能

思路:栈是一种先进后出数据结构,可以利用其特性,制造两个栈,一个保存前进的网页地址,一个保存后退的网页地址,在用一个参数保存当前网页地址。
用栈实现浏览器的前进后退功能_第1张图片
后退两步
用栈实现浏览器的前进后退功能_第2张图片
前进一步
用栈实现浏览器的前进后退功能_第3张图片
Talk is cheap show me the code

public class BrowserStack {
    static String[] url={"http://www.baidu.com","https://mp.csdn.net","https://baike.baidu.com","https://www.google.com"};

    private String currentPage; //当前页
    private LinkedListStack<String> forwardStack=new LinkedListStack<>();  //保存前进网页地址
    private LinkedListStack<String> backStack=new LinkedListStack<>();      //保存后退的网页地址

    //打开
    public void open(String url){
        if (this.currentPage!=null){  //判断当前页是否为空,不为空就就清空 前进栈,把当前网网址存在 后退栈
            this.forwardStack.clear();
            this.backStack.push(this.currentPage);
        }
        show(url,"open");
    }
    //前进
    public void doForward(){
        if (!this.forwardStack.isEmpty()){
            String url=this.forwardStack.pop();
            this.backStack.push(this.currentPage);
            show(url,"doForward");
        }
    }

    //后退
    public void doBack(){
        if (!this.backStack.isEmpty()){  //判断后退栈是否为空
            String url=this.backStack.pop();
            this.forwardStack.push(this.currentPage);
            show(url,"doBack");
        }
    }
	//显示当先页
    public void show(String url,String prefix){
        this.currentPage=url;
        System.out.println(prefix+":"+url);
    }

    public static void main(String[] args) {
        BrowserStack browserStack=new BrowserStack();
        browserStack.open(url[0]);
        browserStack.open(url[1]);
        browserStack.open(url[2]);
        browserStack.open(url[3]);

        browserStack.doForward(); 
        browserStack.doBack();
        browserStack.doForward();
        browserStack.doBack();
        browserStack.doBack();
        browserStack.doForward();
    }
}
结果:
open:http://www.baidu.com
open:https://mp.csdn.net
open:https://baike.baidu.com
open:https://www.google.com

doBack:https://baike.baidu.com
doForward:https://www.google.com
doBack:https://baike.baidu.com
doBack:https://mp.csdn.net
doForward:https://baike.baidu.com

链式栈

public class LinkedListStack<T> {
    private Node head;
    private int count;

    public LinkedListStack(){
        this.count=0;
    }

    //压入
    public boolean push(T value){
        if(this.head==null) {
            this.head=new Node(value,null);
        }
        else{
            Node newNode=new Node(value,null);
            newNode.next=this.head;
            this.head=newNode;
        }
        this.count++;
        return true;
    }

    public void clear(){
        this.head=null;
        this.count=0;
    }

    public  boolean isEmpty(){
        if (this.count==0)
            return true;
        return false;
    }

    //弹出
    public T pop(){
        if (this.count==0) throw new NullPointerException("没了");
        T data= (T) this.head.data;
        this.head=this.head.next;
        --count;
        return data;
    }

    //节点
    private class Node<T>{
        public T data;
        public Node next;

        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }

}

你可能感兴趣的:(简单算法,数据结构,算法)