• 栈的实现 与 两个栈实现队列/两个队列实现栈


    栈的实现

    /**
     * 栈的实现:数组
     */
    class MyStack1<T>{
        private T[] data;
        private int maxLength;
        private int top;
    
        public MyStack1(int maxLength) {
            this.maxLength = maxLength;
            this.data= (T[])new Object[maxLength];
            top=-1;
        }
    
        public boolean isFull(){
            if(top==maxLength-1)return true;
            return false;
        }
        public boolean isEmpty(){
            if(top<=-1)return true;
            return false;
        }
    
        public boolean push(T t){
            if(!isFull()) {data[++top]=t;return true;}
            return false;
        }
        public T pop(){
            if(!isEmpty()){return data[top--];}
            return null;
        }
    }
    
    /**
     * 栈的实现:链表(插入到链表头,并从头取)
     */
    class MyStack2<T>{
        private Node<T> head;
        private int size;
        private int maxSize;
        private class Node<T>{
            private T data;
            private Node<T> next;
    
            public Node(T data) {
                this.data = data;
            }
        }
    
        public MyStack2(int maxSize) {
            this.maxSize = maxSize;
        }
    
        public boolean isEmpty(){
            if(size<=0)return true;
            return false;
        }
            public boolean isFull(){
            if(size>=maxSize)return true;
            return false;
        }
        public boolean push(T t){
            if(!isFull()){
                Node node = new Node(t);
                node.next=head;
                head=node;
                size++;
            }
            return false;
        }
        public T pop(){
            if(!isEmpty()){
                T data = head.data;
                head=head.next;
                size--;
                return data;
            }
            return null;
        }
    }
    

    两个栈实现队列

    /**
     *使用栈实现队列的下列操作:
     *
     * push(x) -- 将一个元素放入队列的尾部。
     * pop() -- 从队列首部移除元素。
     * peek() -- 返回队列首部的元素。
     * empty() -- 返回队列是否为空。
     */
    class MyQueue {
        private Stack<Integer> stack1;
        private Stack<Integer> stack2;
        /** Initialize your data structure here. */
        public MyQueue() {
            this.stack1 = new Stack();
            this.stack2 = new Stack();
        }
    
        /** Push element x to the back of queue. */
        public void push(int x) {
            stack1.push(x);
        }
    
        /** Removes the element from in front of queue and returns that element. */
        public int pop() {
            if(!empty()){
                if(!stack2.empty())return stack2.pop();
                while(!stack1.empty())stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    
        /** Get the front element. */
        public int peek() {
            if(!empty()){
                if(!stack2.empty())return stack2.peek();
                while(!stack1.empty())stack2.push(stack1.pop());
            }
            return stack2.peek();
        }
    
        /** Returns whether the queue is empty. */
        public boolean empty() {
            if(stack1.empty() && stack2.empty())return true;
            return false;
        }
    }
    

    两个队列实现栈

    class MyStack {
        private Queue<Integer> list1;
        private Queue<Integer> list2;
    
        /**
         * Initialize your data structure here.
         */
        public MyStack() {
            this.list1 = new LinkedList();
            this.list2 = new LinkedList();
        }
    
        /**
         * Push element x onto stack.
         */
        public void push(int x) {
            list2.add(x);
            while (!list1.isEmpty()) list2.add(list1.remove());
            Queue<Integer> temp = list1;
            list1 = list2;
            list2 = temp;
        }
    
        /**
         * Removes the element on top of the stack and returns that element.
         */
        public int pop() {
            return list1.remove();
        }
    
        /**
         * Get the top element.
         */
        public int top() {
            return list1.peek();
        }
    
        /**
         * Returns whether the stack is empty.
         */
        public boolean empty() {
            return list1.isEmpty();
        }
    }
    
  • 相关阅读:
    Codeforces 1491 D. Zookeeper and The Infinite Zoo (二进制处理)
    Codeforces 1479A. Searching Local Minimum(注意输入+二分)
    Codeforces 1480B. The Great Hero(阅读模拟题,注意数据范围和攻击顺序)
    Codeforces 1480A. Yet Another String Game (阅读理解题)
    windows 10 开启全盘瞬间索引功能
    JetBrains CLion C++ IDE连接wsl2(Ubuntu)时,报错"Unable to establish SSL connection"解决方案
    WorkFlowy 的 MarkDown 预览方案解决
    git 学习 完全学懂
    jeecgboot <j-popup
    面试之类加载器
  • 原文地址:https://www.cnblogs.com/loveer/p/11757359.html
Copyright © 2020-2023  润新知