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();
            }
            
        }
    }
  • 相关阅读:
    Lua_第 20 章 IO库
    maven具体解释之坐标与依赖
    用python做自己主动化測试--对Java代码做单元測试 (1)
    OSG粒子系统应用:雨雪效果
    Snort:Barnyard2+MySQL+BASE 基于Ubuntu 14.04SNORT
    shiro高速入门
    解决Cocos项目中遇到的fatal error c1083(无法打开包含文件)
    解决TIME_WAIT过多造成的问题
    Web后端语言模拟http请求(带username和password)实例代码大全
    Python
  • 原文地址:https://www.cnblogs.com/javastart/p/5948744.html
Copyright © 2011-2022 走看看