11-双端队列

2021-08-16  本文已影响0人  weyan

代码实现:
package com.weyan;

import com.weyan.list.CircleLinkedList;
import com.weyan.list.List;

public class Deque<E> {
    private List<E> list = new CircleLinkedList<>();
    //元素个数
        public int size() {
            return list.size();
        }
        
        //队列是否为空
        public boolean isEmpty() {
            return list.isEmpty();
        }
        
        //从队尾入队
        public void enQueueRear(E element) {
            list.add(element);
        }
        
        //从队尾出队
        public E deQueueRear() {
            return list.remove(list.size()-1);
        }
        
        //从队头入队
        public void enQueueFront(E element) {
            list.add(0, element);
        }
        
        //从队头出队
        public E deQueueFront() {
            return list.remove(0);
        }
        
        //队头元素
        public E front() {
            return list.get(0);
        }
        
        //队尾元素
        public E rear() {
            return list.get(list.size()-1);
        }
}

验证结果:

上一篇 下一篇

猜你喜欢

热点阅读