栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于双向链表实现: 栈 队列

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于双向链表实现: 栈 队列

 基于双向链表实现栈与队列:

        内部类DoublelinkedList 可以构建双向链表, 提供4中操作: 头插 ,头取 ,尾插 ,尾取 .所以可以称为"双端双向链表"(自己起的 便于理解),这两端分别由head 和 tail 分别引用. 通常链表无论是单向还是双向链表都只有一个 head, 但为了实现基于"双端双向链表"的 "栈" 和 "队列",这四种操作头插 和 头取 就是 "栈" ,头插 尾取就是"队列".

         栈和队列都是逻辑上的结构,在数据结构中是ADT,实现的方式可以基于数组或链表或其它.      


public class StackOrQueuebaseonlink {

    private static class MyStack{
        private final DoublelinkedList linkedList = new DoublelinkedList<>();
        public void push(T t){
            linkedList.addToHead(t);
        }
        public T pop(){
            return linkedList.popFromHead();
        }
    }

    private static class MyQueue{
        private final DoublelinkedList linkedList = new DoublelinkedList<>();
        public void push(T t){
            linkedList.addToHead(t);
        }
        public T pop(){
            return linkedList.popFromTail();
        }
    }

    
    private static class DoublelinkedList {
        public Node head;
        public Node tail;

        
        public T popFromHead(){
            if(this.head == null){
                return null;
            }
            Node curr = head;
            if(head == tail){
                head = null;
                tail = null;
            }else{
                head = head.next;
                head.prev = null;
                curr.next = null;
            }
            return curr.val;
        }
        
        public T popFromTail(){
            if(this.tail == null){
                return null;
            }
            Node curr = tail;
            if(tail == head){
                tail = null;
                head = null;
            }else{
                tail = tail.prev;
                tail.next = null;
                curr.prev = null;
            }
            return curr.val;
        }

        public void addToHead(T val) {
            Node curr = new Node<>(val);
            if (head == null) {
                head = curr;
                tail = curr;
            } else {
                //头插curr
                curr.next = head;
                head.prev = curr;
                //重置head
                head = curr;
            }
        }

        public void addToTail(T val){
            Node curr = new Node<>(val);
            if(tail == null){
                head = curr;
                tail = curr;
            }else{
                //尾插
                tail.next = curr;
                curr.prev = tail;
                tail = curr;
            }
        }
    }

    private static class Node {
        public T val;
        public Node prev;
        public Node next;

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

  左神算法学习

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/764154.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号