数据结构与算法-栈

使用数组和链表实现栈,比较简单,代码如下:

package com.freshbin.dataStructAndAlgo.chapter06.mycode.stack;

/**
 * 使用数组实现栈
 * @author freshbin
 * @date 2020/4/14 9:45
 */
public class StackBasedOnArray {
    private Integer maxSize;
    private Integer currentCount;
    private String[] array;

    public StackBasedOnArray() {
        this.maxSize = 2;
        array = new String[maxSize];
        currentCount = 0;
    }

    public StackBasedOnArray(Integer maxSize) {
        this.maxSize = maxSize;
        array = new String[maxSize];
        currentCount = 0;
    }

    public void push(String value) {
        String returnValue = null;
        // 如果栈已经满了,那么就扩容2倍
        if(currentCount == maxSize) {
            System.out.println("栈满,当前容量为:" + this.maxSize + ",开始扩容2倍");
            expandArraySize();
            System.out.println("扩容后的容量:" + this.maxSize);
        }
        this.array[currentCount++] = value;
    }

    public String pop() {
        if(currentCount == 0) {
            System.out.println("栈空");
            return null;
        }
        return this.array[--currentCount];
    }

    private void expandArraySize() {
        this.maxSize = this.maxSize * 2;
        String[] newArray = new String[this.maxSize];
        for(int i = 0; i < this.array.length; i++) {
            newArray[i] = this.array[i];
        }
        this.array = newArray;
    }

    public static void main(String[] arg) {
        String[] paramDatas = new String[4];
        for(int i = 0; i < paramDatas.length; i++) {
            paramDatas[i] = "param" + i;
        }

        StackBasedOnArray stackBasedOnArray = new StackBasedOnArray();
        stackBasedOnArray.pop();
        for(String paramData : paramDatas) {
            stackBasedOnArray.push(paramData);
        }

        System.out.println("出栈,打印栈中的值:");
        String value = stackBasedOnArray.pop();
        while(value != null) {
            System.out.print(value + " ");
            value = stackBasedOnArray.pop();
        }
    }
}
package com.freshbin.dataStructAndAlgo.chapter06.mycode.stack;

/**
 * 使用链表实现栈
 * @author freshbin
 * @date 2020/4/14 9:45
 */
public class StackBasedOnLinkedList {
    private Node headNode;

    public StackBasedOnLinkedList() {
    }


    public StackBasedOnLinkedList(String value) {
        this.headNode = new Node(value);
    }

    public Node getTop() {
        return this.headNode;
    }

    public void push(String value) {
        Node newNode = new Node(value);
        if(this.headNode == null) {
            this.headNode = newNode;
            return;
        }

        newNode.next = this.headNode;
        this.headNode = newNode;
    }

    public String pop() {
        if(this.headNode == null) {
            System.out.println("栈空!");
            return null;
        }

        String returnValue = this.headNode.data;
        this.headNode = this.headNode.next;
        return returnValue;
    }

    public void clear() {
        this.headNode = null;
    }

    public class Node {
        private String data;
        private Node next;

        public Node(String value) {
            this.data = value;
            this.next = null;
        }

        public String getData() {
            return this.data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public Node getNext() {
            return this.next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

    public static void main(String[] arg) {
        String[] paramDatas = new String[4];
        for(int i = 0; i < paramDatas.length; i++) {
            paramDatas[i] = "param" + i;
        }

        StackBasedOnLinkedList stackBasedOnLinkedList = new StackBasedOnLinkedList();
        System.out.println("出栈数据:" + stackBasedOnLinkedList.pop());
        stackBasedOnLinkedList.push("test");
        System.out.println("出栈数据:" + stackBasedOnLinkedList.pop());
        System.out.println("出栈数据:" + stackBasedOnLinkedList.pop());

        for(int i = 0; i < paramDatas.length; i++) {
            stackBasedOnLinkedList.push("test" + i);
        }

        System.out.print("所有数据出栈,数据:");
        String value = stackBasedOnLinkedList.pop();
        while(value != null) {
            System.out.print(value + " ");
            value = stackBasedOnLinkedList.pop();
        }
    }
}

使用栈实现浏览器的前进后退,虽然也是简单,但是写起来的时候,一开始有点思维错乱了,代码如下:

package com.freshbin.dataStructAndAlgo.chapter06.mycode.stack;

/**
 * 用栈实现浏览器前进后退效果
 * 一开始写得十分乱,而且还有问题,后来就瞄了一下大佬们的答案了
 * @author freshbin
 * @date 2020/4/14 10:37
 */
public class SampleBrowser {

    private String currentUrl;
    private StackBasedOnLinkedList backwardStack;
    private StackBasedOnLinkedList forwardStack;

    public SampleBrowser() {
        backwardStack = new StackBasedOnLinkedList();
        forwardStack = new StackBasedOnLinkedList();
    }

    public String openUrl(String openUrl) {
        if(this.currentUrl != null) {
            this.backwardStack.push(this.currentUrl);
            this.forwardStack.clear();
        }

        this.currentUrl = openUrl;
        showUrl("openUrl");
        return openUrl;
    }

    public String forwardUrl() {
        if(canForwardUrl()) {
            String forwardUrl = this.forwardStack.pop();
            this.backwardStack.push(this.currentUrl);
            this.currentUrl = forwardUrl;
            showUrl("forwardUrl");
        }
        return this.currentUrl;
    }

    public Boolean canForwardUrl() {
        if(this.forwardStack.getTop() != null) {
            return true;
        }
        System.out.println("不能前进!");
        return false;
    }

    public String backwardUrl() {
        if(canBackwardUrl()) {
            String backwardUrl = this.backwardStack.pop();
            this.forwardStack.push(this.currentUrl);
            this.currentUrl = backwardUrl;
            showUrl("backwardUrl");
        }
        return this.currentUrl;
    }

    public Boolean canBackwardUrl() {
        if(this.backwardStack.getTop() != null) {
            return true;
        }
        System.out.println("不能后退!");
        return false;
    }

    public void showUrl(String opt) {
        System.out.println(opt + ":" + this.currentUrl);
    }

    public String getCurrentUrl() {
        System.out.println("当前url:" + this.currentUrl);
        return this.currentUrl;
    }

    public static void main(String[] arg) {
        String a = "a";
        String b = "b";
        String c = "c";
        String d = "d";
        SampleBrowser sampleBrowser = new SampleBrowser();
        String currentUrl = null;

        // 测试步骤
        // 1、依次点击:前进,后退,新页面a
        // 期望值:不允许前进;不允许后退;显示新页面a
        System.out.println("=======1======");
        currentUrl = sampleBrowser.forwardUrl();
        currentUrl = sampleBrowser.backwardUrl();
        currentUrl = sampleBrowser.openUrl(a);

        System.out.println("=======2======");
        // 2、依次点击:前进,后退,新页面b
        // 期望值:不允许前进;不允许后退;显示新页面b
        currentUrl = sampleBrowser.forwardUrl();
        currentUrl = sampleBrowser.backwardUrl();
        currentUrl = sampleBrowser.openUrl(b);

        System.out.println("======3=======");
        // 3、依次点击:前进,后退,新页面c
        // 期望值:不允许前进;允许后退并显示后退的页面a;显示新页面c
        currentUrl = sampleBrowser.forwardUrl();
        currentUrl = sampleBrowser.backwardUrl();
        currentUrl = sampleBrowser.openUrl(c);

        System.out.println("======4=======");
        // 4、依次点击:后退,前进,新页面d
        // 期望值:允许后退并显示后退的页面a;允许前进显示新页面c;显示新页面d
        currentUrl = sampleBrowser.backwardUrl();
        currentUrl = sampleBrowser.forwardUrl();
        currentUrl = sampleBrowser.openUrl(d);

        System.out.println("======5=======");
        // 5、依次点击:前进,后退,后退,后退
        // 期望值:不允许前进,后退到页面c,页面a,不允许后退。
        currentUrl = sampleBrowser.forwardUrl();
        currentUrl = sampleBrowser.backwardUrl();
        currentUrl = sampleBrowser.backwardUrl();
        currentUrl = sampleBrowser.backwardUrl();

        System.out.println("=======显示最后的页面=======");
        sampleBrowser.getCurrentUrl();
    }
}

github地址:https://github.com/freshbin/dataStructAndAlgo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值