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

思路:栈是一种先进后出数据结构,可以利用其特性,制造两个栈,一个保存前进的网页地址,一个保存后退的网页地址,在用一个参数保存当前网页地址。
hyc
后退两步
hyc
前进一步
hyc
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;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值