数据结构与算法(三)队列和栈的实现

三、队列和栈的实现

1、队列实现MyQueue(先进先出):
  1. 链表实现队列:
public static class Node<V> {
        public V value;
        public Node<V> next;

        public Node(V v) {
            value = v;
            next = null;
        }
    }
public static class MyQueue<V> {
        private Node<V> head;
        private Node<V> tail;
        private int size;

        public MyQueue() {
            head = null;
            tail = null;
            size = 0;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public int size() {
            return size;
        }

        public void offer(V value) {
            Node<V> node = new Node<V>(value);
            if (tail == null) {
                head = node;
                tail = node;
            } else {
                tail.next = node;
                tail = node;
            }
            size ++;
        }

        public V poll() {
            V ans = null;
            if (head != null) {
                ans = head.value;
                head = head.next;
                size --;
            }
            if (head == null) {
                tail = null;
            }
            return ans;
        }

        public V peek() {
            V ans = null;
            if (head != null) {
                ans = head.value;
            }
            return ans;
        }
    }
  1. 数组实现队列:
public static class MyQueue {
        private int[] arr;
        private int pushi; // end
        private int polli; // begin
        private int size;
        private final int limit;

        public MyQueue(int limit) {
            arr = new int[limit];
            pushi = 0;
            polli = 0;
            size = 0;
            this.limit = limit;
        }

        public void push(int value) {
            if (size == limit) {
                throw new RuntimeException("队列满了,不能再添加了!");
            }
            arr[pushi] = value;
            size++;
            pushi = nextIndex(pushi);
        }

        public int pop() {
            if (size == 0) {
                throw new RuntimeException("队列为空,无数据!");
            }
            int value = arr[polli];
            size--;
            nextIndex(polli);
            return value;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        private int nextIndex(int index) {
            return index < limit -1 ? index+1 : 0;
        }
    }
2、栈MyStack的实现(先进后出):
  1. 链表:
    public static class MyStack<V> {
            private Node<V> head;
            private int size;
    
            public MyStack() {
                head = null;
                size = 0;
            }
    
            public boolean isEmpty() {
                return size == 0;
            }
    
            public int size() {
                return size;
            }
    
            public void push(V value) {
                Node<V> node = new Node<>(value);
                if (head == null) {
                    head = node;
                }else {
                    node.next = head;
                    head = node;
                }
                size ++;
            }
    
            public V pop() {
                V ans = null;
                if (head != null) {
                    ans = head.value;
                    head = head.next;
                    size--;
                }
                return ans;
            }
    
            public V peek() {
                V ans = null;
                if (head != null) {
                    ans = head.value;
                }
                return ans;
            }
        }
    
  2. 数组:
public static class MyStack {
        private int[] arr;
        private int pushi;
        private int polli;  
        private int size;
        public final int limitStack;

        public MyStack(int limit) {
            arr = new int[limit];
            pushi = 0;
            polli = 0;
            size = 0;
            limitStack = limit;
        }

        public void push(int value) {
            if (size < limitStack) {
                arr[pushi] = value;
                pushi =(pushi+1)%limitStack;
                size ++;
            } else {
                throw new RuntimeException("栈满了!");
            }
        }

        public int poll() {
            if (size == 0) {
                throw new RuntimeException("当前栈为空!");
            }else {
                int ans = arr[polli];
                polli =(polli+1)%limitStack;
                size--;
                return ans;
            }
        }

        public boolean isEmpty() {
            return size==0;
        }
        
    }
双端链表实现双端队列Deque:
	public static class Node<V> {
        public V value;
        public Node<V> last;
        public Node<V> next;

        public Node(V value) {
            this.value = value;
            last = null;
            next = null;
        }
    }

    public static class MyDeque<V> {
        private Node<V> head;
        private Node<V> tail;
        private int size;

        public MyDeque() {
            head = null;
            tail = null;
            size = 0;
        }

        public boolean isEmpty() {
            return size == 0;
        }
        public int size() {
            return size;
        }

        public void pushHead(V value) {
            Node<V> node = new Node<>(value);
            if (head == null) {
                head = node;
                tail = node;
            } else {
                head.last = node;
                node.next = head;
                head = node;
            }
            size ++;
        }

        public void pushTail(V value) {
            Node<V> node = new Node<>(value);
            if (head == null) {
                head = node;
                tail = node;
            } else {
                tail.next = node;
                node.last = tail;
                tail = node;
            }
            size ++;
        }

        public V pollHead() {
            V ans = null;
            if (head == null) {
                return null;
            }
            size --;
            ans = head.value;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = head.next;
                head.last = null;
            }
            return ans;
        }

        public V pollTail() {
            V ans = null;
            if (head == null) {
                return ans;
            }
            size --;
            ans = tail.value;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                tail = tail.last;
                tail.next = null;
            }
            return ans;
        }
        public V peekHead() {
            V ans = null;
            if (head != null) {
                ans = head.value;
            }
            return ans;
        }

        public V peekTail() {
            V ans = null;
            if (head != null) {
                ans = tail.value;
            }
            return ans;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值