• 第11天 Stack Queue


    1.Stack

    package algs4;
    
    import java.util.Iterator;
    import java.util.NoSuchElementException;
    
    public class Stack<Item> implements Iterable<Item> {
    
        private Node<Item> first;
        private int n;
        
        private static class Node<Item> {
            private Item item;
            private Node<Item> next;
        }
        
        public Stack() {
            first = null;
            n = 0;
        }
        
        public boolean isEmpty() {
            return first == null;
        }
        
        public int size() {
            return n;
        }
        
        public void push(Item item) {
            Node<Item> oldfirst = first;
            first = new Node<Item>();
            first.item = item;
            first.next = oldfirst;
            n++;
        }
        
        public Item pop() {
            if (isEmpty()) throw new NoSuchElementException("Stack underflow");
            Item item = first.item;
            first = first.next;
            n--;
            return item;
        
        }
        
        public Item peek() {
            if (isEmpty()) throw new NoSuchElementException("Stack underflow");
            return first.item;
        }
        
        public String toString() {
            StringBuilder s = new StringBuilder();
            for (Item item : this)
                s.append(item + " ");
            return s.toString();
        }
        
        @Override
        public Iterator<Item> iterator() {
            // TODO Auto-generated method stub
            return new ListIterator<Item>(first);
        }
    
        private class ListIterator<Item> implements Iterator<Item> {
            private Node<Item> current;
            
            public ListIterator(Node<Item> first) {
                // TODO Auto-generated constructor stub
                current = first;
            }
    
            @Override
            public boolean hasNext() {
                // TODO Auto-generated method stub
                return current != null;
            }
    
            @Override
            public Item next() {
                // TODO Auto-generated method stub
                if (!hasNext()) throw new NoSuchElementException();
                Item item = current.item;
                current = current.next;
                return item;
            }
    
            @Override
            public void remove() {
                // TODO Auto-generated method stub
                throw new UnsupportedOperationException();
            }
            
        }
        
        public static void main(String[] args) {
            Stack<String> stack = new Stack<String>();
    //        while (!StdIn.isEmpty()) {
    //            
    //        }
        }
        
    }

    2.Queue

    package algs4;
    
    import java.util.Iterator;
    import java.util.NoSuchElementException;
    
    
    
    
    
    
    public class Queue<Item> implements Iterable<Item> {
        private Node<Item> first;
        private Node<Item> last;
        private int n;
        
        
        private static class Node<Item> {
            private Item item;
            private Node<Item> next;
        }
        
        public Queue() {
            first = null;
            last = null;
            n = 0;
        }
        
        public boolean isEmpty() {
            return first == null;
        }
        
        public int size() {
            return n;
        }
        
        public Item peek() {
            if (isEmpty()) throw new NoSuchElementException("Stack underflow");
            return first.item;
        }
        
        public void enqueue(Item item) {
            Node<Item> oldlast = last;
            last = new Node<Item>();
            last.item = item;
            last.next = null;
            if (isEmpty()) first = last;
            else oldlast.next = last;
            n++;
        }
        
        public Item dequeue() {
            if (isEmpty()) throw new NoSuchElementException("Stack underflow");
            Item item = first.item;
            first = first.next;
            n--;
            if (isEmpty()) last = null;
            return item;
        }
        
        
    
        public String toString() {
            StringBuilder s = new StringBuilder();
            for (Item item : this)
                s.append(item + " ");
            return s.toString();
        }
        
        @Override
        public Iterator<Item> iterator() {
            // TODO Auto-generated method stub
            return new ListIterator<Item>(first);
        }
    
        private class ListIterator<Item> implements Iterator<Item> {
            private Node<Item> current;
            
            public ListIterator(Node<Item> first) {
                // TODO Auto-generated constructor stub
                current = first;
            }
    
            @Override
            public boolean hasNext() {
                // TODO Auto-generated method stub
                return current != null;
            }
    
            @Override
            public Item next() {
                // TODO Auto-generated method stub
                if (!hasNext()) throw new NoSuchElementException();
                Item item = current.item;
                current = current.next;
                return item;
            }
    
            @Override
            public void remove() {
                // TODO Auto-generated method stub
                throw new UnsupportedOperationException();
            }
            
        }
    }
  • 相关阅读:
    如何正确夸奖孩子
    C# datatable分页和 list 分页
    js修改Switchery复选框的状态
    虚拟机中centos中设置固定IP
    CommonJS和ES6
    npm使用淘宝镜像
    RabbitMQ基础概念详细介绍
    Web漏洞扫描神器Nikto使用指南
    Redis基本使用
    ROS文件系统导览
  • 原文地址:https://www.cnblogs.com/javastart/p/5948744.html
Copyright © 2020-2023  润新知