• 队列,栈


    队列(FIFO)

    先进先出
    

    数组实现

    判断实现
    public class MyCircularQueue02 {
    
        private int[] data;
        private int head;
        private int tail;
        private int size;
    
    
        public MyCircularQueue02(int k) {
            data = new int[k];
            head = -1;
            tail = -1;
            size = k;
        }
    
    
        public boolean enQueue(int value) {
            if (isFull() == true) {
                return false;
            }
            if (isEmpty() == true) {
                head = 0;
            }
            tail = (tail + 1) % size;
            data[tail] = value;
            return true;
        }
    
        public boolean deQueue() {
            if (isEmpty() == true) {
                return false;
            }
            if (head == tail) {
                head = -1;
                tail = -1;
                return true;
            }
            head = (head + 1) % size;
            return true;
        }
    
    
        public int Front() {
            if (isEmpty() == true) {
                return -1;
            }
            return data[head];
        }
    
    
        public int Rear() {
            if (isEmpty() == true) {
                return -1;
            }
            return data[tail];
        }
    
    
        public boolean isEmpty() {
            return head == -1;
        }
    
    
        public boolean isFull() {
            return ((tail + 1) % size) == head;
        }
    
    }
    
    
    采用 牺牲一个空间实现
    public class MyCircularQueue01 {
        private int[] data;
        private int head;
        private int tail;
        private int size;
    
        public MyCircularQueue01(int size) {
            this.size = size;
            data = new int[size + 1];
            head = 0;
            tail = 0;
        }
    
        public boolean enQueue(int value) {
            if (isFull()) {
                return false;
            }
            data[tail] = value;
            tail = (tail+1)%data.length;
            return true;
        }
    
        public boolean deQueue() {
            if (isEmpty()) {
                return false;
            }
            int pop = data[head];
            head = (head+1)%data.length;
            return true;
        }
    
        public boolean isEmpty() {
            return head == tail;
        }
    
        public boolean isFull() {
            return (tail  + 1) % data.length == head;
       }
        /** Get the front item from the queue. */
        public int Front() {
    
            if (isEmpty()) {
                return -1;
            }
            return data[head];
        }
    
        /** Get the last item from the queue. */
        public int Rear() {
            if (isEmpty()) {
                return -1;
            }
            return data[tail];
        }
     
    }
    

    链表实现

    public class Queue<Item> implements Iterable<Item> {
        private Node first; //指向头
        private Node last;//指向最近添加的元素
        private int N;//元素个数
        private class Node {
            Item item;
            Node next;
        }
    
        public boolean isEmpty() {
            return first == null;
        }
    
        public int size() {
            return N;
        }
    
        public void enqueue(Item item) {
            //添加到队列末尾
            Node oldlast = last;
            last = new Node();
            last.item = item;
            last.next = null;
            if (isEmpty()) {
                first = last;
            } else {
                //队列不为空,添加到末尾节点后面
                oldlast.next = last;
            }
            N++;
        }
    
        public Item dequeue() {
            Item item = first.item;
            first = first.next;
            if (isEmpty()) {
                last = null;
            }
            N--;
            return item;
        }
        public Iterator<Item> iterator() {
            return new listIterator();
        }
    
        public class listIterator implements Iterator<Item> {
            private Node current = first;
    
    
            @Override
            public boolean hasNext() {
                return current != null;
            }
    
            @Override
            public Item next() {
                Item item = current.item;
                current = current.next;
                return item;
            }
        }
    
    }
    
    

    后进先出
    

    数组实现

    public class ResizingArrayStack<Item> implements Iterable<Item>{
        private Item[] a;
        private int N;
    
        public ResizingArrayStack() {
            a = (Item[]) new Object[1];
        }
    
        public ResizingArrayStack(int cap) {
            a = (Item[]) new Object[cap];
        }
    
        public boolean isEmpty() {
            return N == 0;
        }
    
        public void push(Item item) {
            if (N == a.length) {
                resize(2 * a.length);
            }
            a[N++] = item;
        }
    
        public Item pop() {
            Item item = a[--N];
            // 处理对象游离
            a[N] = null;
            //数组太大减半
            //当数组长度小于1/4时,长度减半后,状态刚好半满
            if (N > 0 && N == a.length / 4) {
                resize(a.length / 2);
            }
            return item;
        }
    
        //调整数组大小
        public void resize(int max) {
            Item[] temp = (Item[]) new Object[max];
            for (int i = 0; i < N; i++) {
                temp[i] = a[i];
            }
            a = temp;
        }
    
        @Override
        public Iterator<Item> iterator() {
            return null;
        }
    
        private class ReverseArrayIterator implements Iterator<Item> {
            private int i = N;
    
            @Override
            public boolean hasNext() {
                return i > 0;
            }
    
            @Override
            public Item next() {
                return a[i--];
            }
        }
    }
    
    

    链表实现

    public class Stack<Item> implements Iterable<Item> {
        private class Node {
            Item item;
            Node next;
        }
    
        private Node first;//栈顶
        private int N;//元素数量
    
        public boolean isEmpty() {
            return first == null;
        }
    
        public int size() {
            return N;
        }
    
        public void push(Item item) {
            Node oldfirst = first;
            first = new Node();//new 一个新节点放到表头
            first.item = item;
            first.next = oldfirst;
            N++;
        }
    
        public Item pop() {
            Item item = first.item;
            first = first.next;
            N--;
            return item;
        }
        public Iterator<Item> iterator() {
            return new listIterator();
        }
    
        public class listIterator implements Iterator<Item> {
            private Node current = first;
    
    
            @Override
            public boolean hasNext() {
                return current != null;
            }
    
            @Override
            public Item next() {
                Item item = current.item;
                current = current.next;
                return item;
            }
        }
    }
    

    应用

    算法表达式求值

    求((1+2)*3)表达式的值

    public class Evaluate {
        public static double compute(String s) {
            Stack<String> ops = new Stack<>();
            Stack<Double> vals = new Stack<>();
            for (char c : s.toCharArray()) {
                if (c == '(') {
                } else if (c == '+') {
                    ops.push("+");
                } else if (c == '-') {
                    ops.push("-");
                } else if (c == '*') {
                    ops.push("*");
                } else if (c == '/') {
                    ops.push("/");
                } else if (c == ')') {
                    String op = ops.pop();
                    Double v = vals.pop();
                    if (op == "+") {
                        v = vals.pop() + v;
                    }
                    if (op == "-") {
                        v = vals.pop() - v;
                    }
                    if (op == "*") {
                        v = vals.pop() * v;
                    }
                    if (op == "/") {
                        v = vals.pop() / v;
                    }
                    vals.push(v);
                } else {
                    vals.push(Double.valueOf(String.valueOf(c)));
                }
    
            }
            return vals.pop();
        }
    	public static void main(String[] args) {
    	    double v  = compute("((1+2)*3)");
    	    System.out.println(v);
    	}
    }
    
    
  • 相关阅读:
    python网络编程学习笔记(3):socket网络服务器
    Python编码爬坑指南
    ROT13 维基百科,自由的百科全书
    ZODB + Traversal Wiki Tutorial¶
    ZODB programming guide¶
    利用新浪api获取ip归属地 QtSharp 博客园
    用python做了个桌球瞄准器
    Python运维工具介绍1–fabric
    python httplib2 使用代理出错
    第四回 基类中的修饰符,应该根据你对架构的理解去定义它们,没有绝对的
  • 原文地址:https://www.cnblogs.com/aiguozou/p/11393950.html
Copyright © 2020-2023  润新知