使用双向链表和数组分别实现栈和队列(Java语言实现)

一、双向链表实现栈和队列

  1. 双向链表

    比较方便,而且思路清晰的做法是,先使用双向链表实现双向队列。
    双向队列可以在头部或者尾部添加或者弹出元素。

    随后部分调用双向队列的功能,即可实现栈或队列的功能。(栈:只能从头部添加和弹出元素,链表:只能从头部添加元素,从尾部取出元素)。

    // 双向链表
    public static class Node<T> {
        public T value;
        public Node<T> last; // 前驱指针
        public Node<T> next; // 后继指针
        public Node(T data) {
            value = data;
        }
    }
    
    // 用双向链表实现双向队列
    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; // 新加节点的next指针指向head
                head.last = cur; // 原来的头节点的last指针指向新加节点
                head = cur; // 新加节点成为新的头节点
            }
        }
    
        public void addFromTail(T value){
            Node<T> cur = new Node<T>(value);
            if (head == null){
                head = cur;
                tail = cur;
            } else {
                cur.last = tail; // 新加节点的last指针指向tail
                tail.next = cur; // 原来的尾节点的next指针指向尾节点
                tail = cur; // 新加节点成为新的尾节点
            }
        }
    
        public T popFromHead(){
            if (head == null){
                return null;
            }
            Node<T> cur = head; // 获取头节点,用cur接收
            if(head == tail){
                head = null;
                tail = null;
            } else {
                head = head.next; // 现有头节点的后继节点成为新的头节点
                // 获取头节点,分割头节点与队列主体
                cur.next = null; // cur的next指针指向空内存
                head.last = null; // 新头节点的last指针指向空内存
            }
            return cur.value;
        }
    
        public T popFromTail() {
            if (tail == null) {
                return null;
            }
            Node<T> cur = tail; // 获取尾节点,用cur接收
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                tail = tail.last; // 现有尾节点的前驱节点成为新的尾节点
                // 获取尾节点,分割尾节点与队列主体
                cur.last = null; // cur的last指针指向空内存
                tail.next = null; // 新尾节点的next指针指向空内存
            }
            return cur.value;
        }
    
        public boolean isEmpty() {
            return head == null;
        }
    }
    
  2. 部分调用双向队列的功能,即为栈或队列
    2.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();
        }
    }
    

    2.2 队列的实现
    单向队列的实现也就类似:限制只能从双向链表的一段添加,另一端弹出。

    	public static class MyQueue<T>{
        private DoubleEndsQueue<T> queue;
    
        public MyQueue(){
            queue = new DoubleEndsQueue<>();
        }
    
        // 限制只能从头部添加
        public void push(T value){
            queue.addFromHead(value);
        }
    
        // 限制只能从尾部弹出
        public T poll(){
            return queue.popFromTail();
        }
    
        // 判空方法
        public boolean isEmpty(){
            return queue.isEmpty();
        }
    }
    

二、使用数组实现固定长度的栈

这种应用是比较容易理解和进行实现的:
public static class MyStack{
    private int[] arr;
    private int topIndex;
    private final int volume;

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

    public void push(int value){
        if(topIndex == volume){
            throw new RuntimeException("栈已满,无法继续添加");
        }
        topIndex++;
        arr[topIndex-1] = value;
    }

    public int pop(){
        if(topIndex == 0){
            throw new RuntimeException("栈已空,无法继续添加");
        }

        topIndex--;
        int value = arr[topIndex];
        arr[topIndex] = 0;
        return value;
    }
}

三、使用数组实现队列

这里需要用到环形的数组:
使得队列中用于取出元素的下标追赶用于添加元素的下标,当弹出/添加操作成功时,下标加一或回到零,用数组的元素最大数量限制两个下标之间的“差距”,若二者的“差距”等于数组长度,意味着队列已满,若二者的差距为0,意味着队列为空。

public class RingArrayToQueue {
    public static class MyQueue{
        private int[] arr;
        private int pushIndex;
        private int pollIndex;
        private int size;
        private final int limit;

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

        public void push(int value){
            if(size == limit){
                throw new RuntimeException("队列已满,无法继续添加");
            }
            size++;
            arr[pushIndex] = value;
            pushIndex = nextIndex(pushIndex);
        }

        public int poll(){
            if(size == 0){
                throw new RuntimeException("队列已空,无法继续取出");
            }
            size--;
            int value = arr[pollIndex];
            pollIndex = nextIndex(pollIndex);
            return value;

        }

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

四、总结

使用双向链表实现栈和队列的操作是类似的,均为在添加或弹出元素时,对于双向链表的头节点或尾节点,以及与其相邻的前驱和后继之间,将节点的last和next指针的重新排列和定向。

使用数组实现栈的操作较为简单。

而使用数组实现队列,则需要对数组的使用进行一定的设计,方便进行先进先出的数据存取操作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值