zoukankan      html  css  js  c++  java
  • Algs4-1.3.40前移编码

    1.3.40前移编码。从标准输入读取一串字符,使用链表保存这些字符并清除重复字符。当你读取了一个从未见过的字符时,将它插入表头。当你读取了一个重复的字符时,将它从链表中删除并再次插入表头。将你的程序命名为MoveToFront:它实现了著名的前移编码策略,这种策略假设最近访问过的元素很有可能再次访问,因此可以用于缓存、数据压缩等许多场景。
    答:
    图片
    public class MoveToFront<Item>
    {
        private int N;
        private Node first;
        //
        private class Node
        {
            Item item;
            Node next;
        }
        //
        public void insertToFirst(Item item)
        {
                Node oldfirst=first;
                first=new Node();
                first.item=item;
                first.next=oldfirst;
                N++;
             
                Node current=first;
                while(current.next!=null)
                {
                    if(current.next.item.equals(item))
                    {
                        Node repeat=current.next;
                        current.next=current.next.next;
                        repeat.next=null;
                        N--;
                        break;
                    }
                    current=current.next;
                }
          }
       
        public boolean isEmpty()
        {return N==0;}
       
        public int size()
        {return N;}
       
        public void showAll()
        {
            Node current=first;
            while(current!=null)
            {
                StdOut.print(current.item+" ");
                current=current.next;
            }
        }
       
        public static void main(String[] args)
        {
            MoveToFront<String> list=new MoveToFront<String>();
            while(!StdIn.isEmpty())
            {
                String item=StdIn.readString();
                list.insertToFirst(item);
            }
            //
            list.showAll();
        }//end main
    }//end class

  • 相关阅读:
    android 扇形菜单
    Gradle 教程:第一部分,安装【翻译】
    Android Studio 创建虚拟机失败 Failed to load 解决办法
    【翻译】Best Practices for User interface android 适配不同屏幕、不同分辨率
    [转]QT 4.8 静态库编译方法
    使用windows资源管理器打开jar
    一步步理解typedef
    模拟Chrome皮肤
    FAT12格式的引导区实现
    Android-获取Html元素
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9854324.html
Copyright © 2011-2022 走看看