作者: lconcise | 来源:发表于2021-12-31 21:36 被阅读0次

    后进者先出,先进者后出,这就是典型的“栈”结构。

    从栈的操作特性上来看,栈是一种“操作受限”的线性表,只允许在一端插入和删除数据。

    如何实现一个栈

    栈既可以用数组来实现,也可以用链表来实现。用数组实现的栈,我们叫作顺序栈,用链表实现的栈,我们叫作链式栈。

    顺序栈

    /**
     * 基于数组实现的顺序栈.
     *
     * @author: liusj
     * @date: 2021/12/29
     */
    public class ArrayStack {
        // 数组
        private String[] items;
        // 栈中元素的个数
        private int count;
        // 栈的大小
        private int n;
    
        public ArrayStack(int n) {
            this.items = new String[n];
            this.count = 0;
            this.n = n;
        }
    
        /**
         * 入栈.
         *
         * @param item
         * @return
         */
        public boolean push(String item) {
            if (count == n) return false;
            items[count] = item;
            count++;
            return true;
        }
    
        /**
         * 出栈.
         *
         * @return
         */
        public String pop() {
            // 栈为空,返回null
            if (count == 0) return null;
            // 返回下标为count-1的数组元素,并且栈中元素个数count减一
            String item = items[count - 1];
            --count;
            return item;
        }
    }
    

    链式栈

    /**
     * 基于链表实现的栈.
     *
     * @author: liusj
     * @date: 2021/12/30
     */
    public class StackBasedOnLinkedList {
    
        private Node top = null;
    
        public void push(int data) {
            Node newNode = new Node(data, null);
    
            if (top == null) {
                this.top = newNode;
            } else {
                newNode.next = top;
                top = newNode;
            }
        }
    
        public int pop() {
            if (top == null) {
                return -1;
            } else {
                int data = top.data;
                top = top.next;
                return data;
            }
        }
    
        private static class Node {
            private int data;
            private Node next;
    
            public Node(int data, Node next) {
                this.data = data;
                this.next = next;
            }
    
            public int getData() {
                return data;
            }
        }
    }
    

    使用前后栈实现浏览器的前进后退.

    /**
     * 使用前后栈实现浏览器的前进后退.
     *
     * @author: liusj
     * @date: 2021/12/31
     */
    public class SampleBrowser {
    
        public static void main(String[] args) {
            SampleBrowser browser = new SampleBrowser();
            browser.open("http://www.baidu.com");
            browser.open("http://news.baidu.com/");
            browser.open("http://news.baidu.com/ent");
            browser.goBack();
            browser.goBack();
            browser.goForward();
            browser.open("http://www.qq.com");
            browser.goForward();
            browser.goBack();
            browser.goForward();
            browser.goBack();
            browser.goBack();
            browser.goBack();
            browser.goBack();
            browser.checkCurrentPage();
        }
    
        private String currentPage;
        private LinkedListBasedStack backStack;
        private LinkedListBasedStack forwardStack;
    
        public SampleBrowser() {
            this.backStack = new LinkedListBasedStack();
            this.forwardStack = new LinkedListBasedStack();
        }
    
        public void open(String url) {
            if (this.currentPage != null) {
                this.backStack.push(this.currentPage);
                this.forwardStack.clear();
            }
            showUrl(url, "Open");
        }
    
        public boolean canGoBack() {
            return this.backStack.size() > 0;
        }
    
        public boolean canGoForward() {
            return this.forwardStack.size() > 0;
        }
    
        public String goBack() {
            if (this.canGoBack()) {
                this.forwardStack.push(this.currentPage);
                String backUrl = this.backStack.pop();
                showUrl(backUrl, "Back");
                return backUrl;
            }
    
            System.out.println("* Cannot go back, no pages behind.");
            return null;
        }
    
        public String goForward() {
            if (this.canGoForward()) {
                this.backStack.push(this.currentPage);
                String forwardUrl = this.forwardStack.pop();
                showUrl(forwardUrl, "Foward");
                return forwardUrl;
            }
    
            System.out.println("** Cannot go forward, no pages ahead.");
            return null;
        }
    
        public void showUrl(String url, String prefix) {
            this.currentPage = url;
            System.out.println(prefix + " page == " + url);
        }
    
        public void checkCurrentPage() {
            System.out.println("Current page is: " + this.currentPage);
        }
    
        /**
         * A LinkedList based Stack implementation.
         */
        public static class LinkedListBasedStack {
    
            private int size;
            private Node top;
    
            static Node createNode(String data, Node next) {
                return new Node(data, next);
            }
    
            public void clear() {
                this.top = null;
                this.size = 0;
            }
    
            public void push(String data) {
                Node node = createNode(data, this.top);
                this.top = node;
                this.size++;
            }
    
            public String pop() {
                Node popNode = this.top;
                if (popNode == null) {
                    System.out.println("Stack is empty.");
                    return null;
                }
                this.top = popNode.next;
                if (this.size > 0) {
                    this.size--;
                }
                return popNode.data;
            }
    
            public String getTopData() {
                if (this.top == null) {
                    return null;
                }
                return this.top.data;
            }
    
            public int size() {
                return this.size;
            }
    
            public void print() {
                System.out.println("Print stack:");
                Node currentNode = this.top;
                while (currentNode != null) {
                    String data = currentNode.getData();
                    System.out.print(data + "\t");
                    currentNode = currentNode.next;
                }
                System.out.println();
            }
    
            public static class Node {
    
                private String data;
                private Node next;
    
                public Node(String data) {
                    this(data, null);
                }
    
                public Node(String data, Node next) {
                    this.data = data;
                    this.next = next;
                }
    
                public void setData(String data) {
                    this.data = data;
                }
    
                public String getData() {
                    return this.data;
                }
    
                public void setNext(Node next) {
                    this.next = next;
                }
    
                public Node getNext() {
                    return this.next;
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:

          本文链接:https://www.haomeiwen.com/subject/uffnqrtx.html