zoukankan      html  css  js  c++  java
  • 算法(Algorithms)第4版 练习 1.3.26

    方法实现:

    //1.3.26
        /**
         * remove all of the nodes in the list that have key as its item field
         * 
         * @param list the linked list of T
         * @param key the T key
         * 
         * @return void
         * 
         */
        public static <T> void remove(LinkedList<T> list, T key) {
            Node<T> precurrent;
            precurrent = findPreNode(list, key);
            
            //remove all of the nodes
            while(precurrent.next != null) {
                
                if(precurrent.next == list.first)
                    list.first = list.first.next;
                else
                    precurrent.next = precurrent.next.next;
                
                precurrent = findPreNode(list, key);
            }
            
        }
        
        //1.3.26
        /**
         * find the node in the list whose item equals key
         * 
         * @param key the T key
         * 
         * @return return the previous node whose item equals key
         */
        private static <T> Node<T> findPreNode(LinkedList<T> list, T key) {
            Node<T> precurrent = new Node<T>();
            precurrent.next = list.first;
            
            while(precurrent.next != null && !precurrent.next.item.equals(key)) {
                precurrent = precurrent.next;
            }
            
            return precurrent;
        }

    测试用例:

    package com.qiusongde.linkedlist;
    
    import edu.princeton.cs.algs4.StdIn;
    import edu.princeton.cs.algs4.StdOut;
    
    public class Exercise1326 {
    
        public static void main(String[] args) {
            
            String key = "to";
            
            LinkedList<String> list = new LinkedList<String>();
            
            while(!StdIn.isEmpty()) {
                String s = StdIn.readString();
                list.insertAtBeginning(s);
                StdOut.println("insertAtBeginning success: " + s);
                StdOut.println(list);
            }
            
            LinkedList.remove(list, key);
            StdOut.println("remove success:" + key);
            StdOut.println(list);
            
        }
    
    }

    输入数据:

    to
    be
    or
    not
    to

    结果1:

    insertAtBeginning success: to
    to 
    insertAtBeginning success: be
    be to 
    insertAtBeginning success: or
    or be to 
    insertAtBeginning success: not
    not or be to 
    insertAtBeginning success: to
    to not or be to 
    remove success:to
    not or be 

    结果2:

    insertAtBeginning success: to
    to 
    insertAtBeginning success: be
    be to 
    insertAtBeginning success: or
    or be to 
    insertAtBeginning success: not
    not or be to 
    insertAtBeginning success: to
    to not or be to 
    remove success:not
    to or be to 

    结果3:

    insertAtBeginning success: to
    to
    insertAtBeginning success: be
    be to
    insertAtBeginning success: or
    or be to
    insertAtBeginning success: not
    not or be to
    insertAtBeginning success: to
    to not or be to
    remove success:qiu
    to not or be to

  • 相关阅读:
    设置debian6源
    debian7编译安装tengine添加lua和ldap模块
    elasticsearch5使用snapshot接口备份索引
    logstash5生成init脚本后台启动
    Xpack集成LDAP
    debian安装filebeat5.5收集nginx日志
    kibana5画图
    安装Xtrabackup,设置定时备份msyql数据库
    编译安装nrpe,配置监控mysql端口和主从状态
    编译安装keepalived,实现双主mysql高可用
  • 原文地址:https://www.cnblogs.com/songdechiu/p/6512182.html
Copyright © 2011-2022 走看看