zoukankan      html  css  js  c++  java
  • 链表简单实现栈与队列

    链表方式实现栈

    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;
            }
        }
    }
  • 相关阅读:
    ASP.NET MVC中获取URL地址参数的两种写法
    SQL Server之存储过程基础知识
    ASP.NET MVC 四种Controller向View传值方法
    Js数据类型、Json格式、Json对象、Json字符串
    调用微信内置的方法及wx.config的配置问题
    ref和out的使用及区别
    ASP.NET MVC post请求接收参数的三种方式
    Asp.Net Mvc 路由机制
    Asp.Net MVC中Action跳转小结
    JS应用MD5散列计算头像URL
  • 原文地址:https://www.cnblogs.com/fxust/p/8086493.html
Copyright © 2011-2022 走看看