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

  • 相关阅读:
    转发自lealii 的博客,如有侵权请通知删除
    SQL注入详解-转发
    IT实用技术资源整理
    JMeter-接口自动化之正则表达式关联
    转发自_destiny如有侵权请联系删除
    二元数组的最小值最大和
    连续区间和大于等于某个数(弱化版)
    独一无二的最小生成树
    牛客小白月赛19 弱版 C
    牛客 安徽大学新生赛 G
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9854324.html
Copyright © 2011-2022 走看看