• 链表简单实现栈与队列


    链表方式实现栈

    public class StackImpl<Item> implements Iterable<Item> {
        
        private Node first;
        private int N;
        /**
         * 内部类实现链表节点定义
         * @author feng
         *
         */
        private class Node{
            Item item;
            Node next;
        }
        
        public boolean isEmpty(){
            return first == null;
        }
        public int size(){
            return N;
        }
        
        public void push(Item item){
            Node oldFirst = first;
            first = new Node();
            first.item = item;
            first.next = oldFirst;
            N++;
        }
        public Item pop(){
            Item item = first.item;
            first = first.next;
            N--;
            return item;
        }
    
        /**
         * 迭代器
         * @author feng
         *
         */
        @Override
        public Iterator<Item> iterator() {
            return new ListIterator();
        }
        /**
         * 内部类实现迭代器
         * @author feng
         *
         */
        private class ListIterator implements Iterator<Item>{
            private Node current = first;
            @Override
            public boolean hasNext() {
                // TODO Auto-generated method stub
                return current != null;
            }
            public void remove(){}
            @Override
            public Item next() {
                Item item = current.item;
                current = current.next;
                return item;
            }
        }
    
    }

    链表方式实现队列

    public class QueueImpl<Item> implements Iterable<Item> {
    
        private Node first;
        private Node last;
        private int N;
        
        private class Node{
            Item item;
            Node next;
        }
        
        public boolean isEmpty(){
            return first == null;
        }
        
        public int size(){
            return N;
        }
            
        public void enqueue(Item item){
            Node oldlast = last;
            last.item = item;
            last.next = null;
            if(isEmpty()){
                first = last;
            }else{
                oldlast.next = last;
            }
            N++;
        }
        
        public Item dequeue(){
            Item item = first.item;
            first = first.next;
            if(isEmpty()){
                last = null;
            }
            N--;
            return item;
        }
        /**
         * 迭代器
         */
        @Override
        public Iterator<Item> iterator() {
            // TODO Auto-generated method stub
            return new ListIterator();
        }
        
        /**
         * 迭代器实现类
         * @author feng
         *
         */
        private class ListIterator implements Iterator<Item>{
            private Node current = first;
            @Override
            public boolean hasNext() {
                // TODO Auto-generated method stub
                return current != null;
            }
            public void remove(){}
            @Override
            public Item next() {
                Item item = current.item;
                current = current.next;
                return item;
            }
        }
    }
  • 相关阅读:
    Hibernate学习笔记
    Oracle12c 在windonServer2012中安装的步骤
    提升tomcat服务器性能的七条经验
    zyUpload---照片上传并显示效果
    js的隐含参数(arguments,callee,caller)使用方法
    js中callback.call()和callback()的区别
    理解javascript中的回调函数(callback)
    Spring MVC 流程图
    JDBC在springMvc等框架中使用的方式
    为IE8添加EventListener系列方法支持
  • 原文地址:https://www.cnblogs.com/fxust/p/8086493.html
Copyright © 2020-2023  润新知