栈和队列的底层实现-Java描述

栈和队列的两种底层实现-Java描述

一:双向链表实现

双向链表实现可以从头部进,头部出,以及尾部进,尾部出的结构

    public static class Node<T> {
        public T value;
        public Node<T> last;
        public Node<T> next;

        public Node(T value) {
            this.value = value;
        }
    }

    public static class DoubleEndsQueue<T> {
        public Node<T> head;
        public Node<T> tail;

        public void addFromHead(T value) {
            Node<T> cur = new Node<T>(value);
            if (head == null){
                head = cur;
                tail = cur;
            }else {
                cur.next = head;
                head.last = cur;
                head = cur;
            }
        }

        public void addFromBottom(T value) {
            Node<T> cur = new Node<T>(value);
            if (head == null){
                head = cur;
                tail = cur;
            }else {
                cur.last = tail;
                tail.next = cur;
                tail = cur;
            }
        }

        public T popFromHead(T value) {
           if (head == null){
               return null;
           }
           Node<T> cur = head;
           if (head == tail){
               head = null;
               tail = null;
           }else {
               head = head.next;
               cur.next = null;
               head.last = null;
           }
           return cur.value;
        }

        public T popFromBottom(T value) {
            if (head == null){
                return null;
            }
            Node<T> cur = tail;
            if (head == tail){
                head = null;
                tail = null;
            }else {
                tail = tail.last;
                cur.last = null;
                tail.next = null;
            }
            return cur.value;
        }
        
        public boolean isEmpty() {
            return head == null;
        }
    }

   

1.栈

要求只能从头部进、头部出

    public static class MyStack<T> {
        private DoubleEndsQueue<T> queue;

        public MyStack(){
            queue = new DoubleEndsQueue<T>();
        }

        public void push(T value){
            queue.addFromHead(value);
        }

        public T pop(){
            return queue.popFromHead();
        }

        public boolean isEmpty(){
            return queue.isEmpty();
        }
    }

测试

        MyStack<Integer> myStack = new MyStack<>();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        while (!myStack.isEmpty()){
            System.out.println(myStack.pop());
        }

在这里插入图片描述

2.队列

要求只能从头部进、尾部出

   public static class MyQueue<T> {
        private DoubleEndsQueue<T> queue;

        public MyQueue(){
            queue = new DoubleEndsQueue<T>();
        }

        public void push(T value){
            queue.addFromHead(value);
        }

        public T pop(){
            return queue.popFromBottom();
        }

        public boolean isEmpty() {
            return queue.isEmpty();
        }
    }

测试

        MyQueue<Integer> myQueue = new MyQueue<>();
        myQueue.push(1);
        myQueue.push(2);
        myQueue.push(3);
        myQueue.push(4);
        while (!myQueue.isEmpty()){
            System.out.println(myQueue.pop());
        }

在这里插入图片描述

二:数组实现

1.栈

使用数组配合一个指针来完成

public class StackArray {
    public static class MyStack{
        private int[] arr;
        private int cur;
        private int size;
        private final int limit;

        public MyStack(int limit) {
            arr = new int[limit];
            cur = 0;
            this.limit = limit;
        }

        public void push(int value){
            if (cur == limit){
                System.out.println("栈已满!!");
            }
            arr[cur] = value;
            cur++;
        }

        public int pop(){
            if (cur == 0){
                System.out.println("栈已空!!");
            }
            cur--;
            return arr[cur];
        }

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

2.队列

设计一个循环队列

public class RingArray {
    public static class MyQueue {
        private int[] arr;
        private int pushi;
        private int polli;
        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){
                System.out.println("队列已满!");
            }
            size++;
            arr[pushi] = value;
            pushi = nextIndex(pushi);
        }

        public int pop(){
            if (size == 0){
                System.out.println("队列已空!");
            }
            size--;
            int x = arr[polli];
            polli = nextIndex(polli);
            return x;
        }

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

        private int nextIndex(int i) {
            return i < limit - 1 ? i + 1 : 0;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小本科生debug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值