zoukankan      html  css  js  c++  java
  • 数据结构-背包、栈、队列

    背包

    背包是一种不支持从中删除元素的集合数据类型,目的就是帮助用例收集元素并迭代遍历所有收集元素。

    API

    public class Bag<Item> implements Iterable<Item>
        Bag()    创建一个空背包
        void add(Item)    添加一个元素
        boolean isEmpty()    背包是否为空
        int size()    背包中的元素数量
    

     实现

    import java.util.Iterator;
    
    /**
     * 背包实现 基于链表
     * 实现细节和Stack相同
     */
    public class Bag<Item> implements Iterable<Item>{
        private Node first; //链表首节点
        private class Node{
            Item item;
            Node next;
        }
        /**
         * 添加元素
         */
        public void add(Item item){
            //和Stack.push()相同
            //向栈顶添加元素
            Node oldfirst = first;
            first = new Node();
            first.item = item;
            first.next = oldfirst;
        }
        public Iterator<Item> iterator(){
            return new ListIterator();
        }
        private class ListIterator implements Iterator<Item>{
            private Node current = first; //维护一个实例变量几率链表当前节点
            @Override
            public boolean hasNext() {
                return current.next != null;
            }
    
            @Override
            public Item next() {
                Item item = current.item;
                current = current.next;
                return item;
            }
        }
    }
    

      


     队列

    先进先出队列(简称队列)是一种基于先进先出(FIFO)策略的集合类型。当用例使用foreach语句迭代访问队列中的元素时,元素的处理顺序就是它们被添加到队列中的顺序。

    在应用程序使用队列的主要原因是在用集合保存元素的同时保存它们的相对顺序:使它们入列顺序和出列顺序相同。

    API

    public class Queue<Item> implements Iterable<Item>
        Queue()    创建一个空队列
        void enqueue(Item item)    添加一个元素
        Item dequeue()    删除最早添加的队列
        boolean isEmpty()    队列是否为空
        int size()     队列中的元素数量
    

     实现

    /**
     * 队列实现,基于链表实现
     */
    public class Queue<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 = new Node();
            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;
        }
        public Iterator<Item> iterator(){
            return new ListIterator();
        }
        private class ListIterator implements Iterator<Item> {
            private Node current = first; //维护一个实例变量几率链表当前节点
            @Override
            public boolean hasNext() {
                return current.next != null;
            }
    
            @Override
            public Item next() {
                Item item = current.item;
                current = current.next;
                return item;
            }
        }
    }
    

    下压栈(简称栈)是一种基于后进先出(LIFO)策略的集合类型。当用例使用foreach语句迭代访问队列中的元素时,元素的处理顺序和它们被压入的顺序正好相反。

    在应用程序中使用栈迭代器的一个典型原因是在用集合保存元素的同时颠倒它们的相对顺序。

    API

    public class Stack<Item> implements Iterable<Item>
        Stack()    创建一个空栈
        void push(Item item)    添加一个元素
        Item pop()    删除最近添加元素
        boolean isEmpty()    栈是否为空
        int size()    栈中的元素数量

     实现

    /**
     * 下压栈 链表实现
     */
    public class Stack<Item> implements Iterable<Item>{
        private Node first; //栈顶
        private int N; //元素数量
        private class Node{
            Item item;
            Node next;
        }
        /**
         * 栈是否为空
         */
        public boolean isEmpty(){
            return first == null; //或 N==0
        }
        /**
         * 栈中元素数量
         */
        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;
        }
    
        /**
         * 返回栈顶元素
         * @return
         */
        public Item peek(){
            return first.item;
        }
        public Iterator<Item> iterator(){
            return new ListIterator();
        }
        private class ListIterator implements Iterator<Item> {
            private Node current = first; //维护一个实例变量几率链表当前节点
            @Override
            public boolean hasNext() {
                return current.next != null;
            }
    
            @Override
            public Item next() {
                Item item = current.item;
                current = current.next;
                return item;
            }
        }
    }
    

    参考文献

    算法 [美]Robert Sedgewick, kevin Wayne

  • 相关阅读:
    [引]ASP.NET MVC 4 Content Map
    [转]ASP.NET MVC 2: Model Validation
    [转]ASP.NET MVC中你必须知道的13个扩展点
    [转]Best way to sort a DropDownList in MVC3 / Razor using helper method
    [转]Sql Server参数化查询之where in和like实现详解
    [转]Oracle Stored Procedures Hello World Examples
    [转]oracle的ANYDATA数据类型
    重构中对设计模式的反思
    poj2186 Popular Cows --- 强连通
    mac下通过xcodebuild使用oclint
  • 原文地址:https://www.cnblogs.com/xiejinjie/p/10004244.html
Copyright © 2011-2022 走看看