zoukankan      html  css  js  c++  java
  • 第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();
            }
            
        }
    }
  • 相关阅读:
    Numpy技巧
    Date
    Soulwail
    吴裕雄--天生自然python学习笔记:python 用 Open CV抓取脸部图形及保存
    吴裕雄--天生自然python学习笔记:python 用 Open CV 进行人脸识别
    吴裕雄--天生自然python学习笔记:人脸识别用到的特征文件haarcascade_frontalface_default.xml下载
    吴裕雄--天生自然python学习笔记:python OpenCV 基本绘图
    吴裕雄--天生自然python学习笔记:python用OpenCV 读取和显示图形
    吴裕雄--天生自然python学习笔记:python下载安装各种模块的whl文件网址
    吴裕雄--天生自然python学习笔记:python爬虫PM2.5 实时监测显示器
  • 原文地址:https://www.cnblogs.com/javastart/p/5948744.html
Copyright © 2011-2022 走看看