《算法》- 第4版- 1.3章(1.3.18-1.3.28)链表练习

6 篇文章 0 订阅
2 篇文章 0 订阅
/*
 *@program: algStudy
 *@description: 链表节点
 *@author: chensy
 *@create: 2019-07-12 20:59
 */
class ListNode
{
     public int data;
     public ListNode next;
     public ListNode(int data){this.data = data;}

     /*
      * @Author chensy
      * @Description //TODO 删除最后一个节点
      * @Date 23:17 2019/7/12
      * @Param [first]
      * @return ListNode
      **/
     public static ListNode deleteLastNode(ListNode head) {

          if(head == null || head.next == null)
          {
               return head;
          }
          ListNode tmpNode = new ListNode(-1);
          tmpNode.next = head;

          while(head.next.next != null)
          {
               head = head.next;
          }

          head.next = null;

          return tmpNode.next;
     }

     /*
      * @Author chensy
      * @Description //TODO 删除指定索引节点, 索引从0开始
      * @Date 23:17 2019/7/12
      * @Param [node, index]
      * @return ListNode
      **/
     public static ListNode delete(ListNode head, int index) {

          if(head == null)
          {
               return null;
          }

          ListNode tmpNode = new ListNode(-1);
          tmpNode.next = head;


          int count = 1;
          while (head.next != null)
          {
               if(count++ == index)
               {
                    head.next = head.next.next;
               }

               //假若删除项在最后一项
               if(head.next == null)
               {
                    break;
               }

               head = head.next;
          }

          return tmpNode.next;

     }

     /*
      * @Author chensy
      * @Description //TODO 查找item是否存在该链表
      * @Date 23:18 2019/7/12
      * @Param [head, item]
      * @return boolean
      **/
     public static boolean find(ListNode head, int item) {
          if(head == null || (head.next == null && head.data != item))
          {
               return false;
          }

          while ( head.next != null)
          {
               if(head.data == item)
               {
                    return true;
               }
               head = head.next;
          }

          return false;

     }

     /*
      * @Author chensy
      * @Description //TODO 删除node后续节点
      * @Date 23:18 2019/7/12
      * @Param [head, node]
      * @return ListNode
      **/
     public static ListNode removeAfter(ListNode head, ListNode node) {
          if(node == null || head == null || head.data == node.data)
          {
               return null;
          }

          ListNode tmpNode = new ListNode(-1);
          tmpNode.next = head;


          while (head.next != null)
          {
               if(head.data == node.data)
               {
                    head.next = null;
                    break;
               }

               head = head.next;
          }

          return tmpNode.next;
     }

     /*
      * @Author chensy
      * @Description //TODO  合并节点, 右合并到左
      * @Date 23:18 2019/7/12
      * @Param [left, right]
      * @return ListNode
      **/
     public static ListNode insertNode(ListNode left, ListNode right) {
          if(left == null && right == null)
          {
               return null;
          }
          if (left == null) {
               return right;
          }
          if(right == null)
          {
               return left;
          }
          ListNode tmpNode = new ListNode(-1);
          tmpNode.next = left;
          left.next = right;

          return tmpNode.next;
     }

     /*
      * @Author chensy
      * @Description //TODO 删除指定Key的所有节点
      * @Date 23:19 2019/7/12
      * @Param [node, key]
      * @return ListNode
      **/
     public static ListNode remove(ListNode node, int key) {
          if(node == null || (node.next == null && node.data == key))
          {
               return null;
          }

          ListNode tmpNode = new ListNode(-1);
          tmpNode.next = node;

          while(node != null && node.next != null)
          {
               if(node.next.data == key) {
                    node.next = node.next.next;
               }
               //防止删除的是倒数第二个
               if (node.next != null && node.next.data == key)
               {
                    node.next = null;
                    break;
               }

               node = node.next;

          }

          return tmpNode.next;
     }

     /*
      * @Author chensy
      * @Description //TODO 查找链表中的最大值, 若不存在返回0 (循环解法)
      * @Date 13:26 2019/7/13
      * @Param [head]
      * @return int
      **/
     public static int max (ListNode head) {
          if(head == null)
          {
               return 0;
          }

          int max = head.data;
          while (head != null)
          {
               if(head.data > max)
               {
                    max = head.data;
               }
               head = head.next;
          }

          return max;
     }

     /*
      * @Author chensy
      * @Description //TODO 查找链表中的最大值, 若不存在返回0 (递归解法)
      * @Date 13:33 2019/7/13
      * @Param [head]
      * @return int
      **/
     public static int max2(ListNode head) {
          if(head == null)
          {
               return 0;
          }
          return head.next == null ? head.data : max(head.next);
     }
}

 

可以通过以下步骤在Ubuntu上离线安装RabbitMQ: 1. 下载RabbitMQ的deb包和依赖包,可以从官网或者镜像站下载。 2. 将下载的deb包和依赖包复制到Ubuntu系统中。 3. 安装依赖包,可以使用dpkg命令进行安装,例如: ``` sudo dpkg -i erlang-base_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-asn1_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-crypto_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-public-key_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-ssl_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-syntax-tools_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-mnesia_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-runtime-tools_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-inets_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-os-mon_22.3.4.1-1_amd64.deb sudo dpkg -i erlang-xmerl_22.3.4.1-1_amd64.deb ``` 4. 安装RabbitMQ,可以使用dpkg命令进行安装,例如: ``` sudo dpkg -i rabbitmq-server_3.8.9-1_all.deb ``` 5. 启动RabbitMQ服务,可以使用以下命令启动: ``` sudo systemctl start rabbitmq-server ``` 6. 验证RabbitMQ是否安装成功,可以使用以下命令: ``` sudo rabbitmqctl status ``` 如果输出类似于以下内容,则表示RabbitMQ已经成功安装: ``` Status of node rabbit@localhost ... [{pid,1593}, {running_applications,[{rabbit,"RabbitMQ","3.8.9"}, {rabbit_common,[],"3.8.9"}, {xmerl,"XML parser","1.3.18"}, {os_mon,"CPO CXC 138 46","2.4.7"}, {cowboy,"Small, fast, modern HTTP server.","2.8.0"}, {cowlib,"Support library for manipulating Web protocols.","2.9.1"}, {ranch,"Socket acceptor pool for TCP protocols.","1.7.1"}, {ssl,"Erlang/OTP SSL application","10.6.2"}, {public_key,"Public key infrastructure","1.10.4"}, {asn1,"The Erlang ASN1 compiler version 5.0.8","5.0.8"}]}, {os,{unix,linux}}, {erlang_version,"Erlang/OTP 22 [erts-10.7.2] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:64] [hipe] [dtrace]\n"}, {memory,[{total,37437904}, {connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,0}, {queue_procs,0}, {queue_slave_procs,0}, {plugins,0}, {other_proc,2147483647}, {mnesia,0}, {mgmt_db,0}, {msg_index,0}, {other_ets,0}, {binary,0}, {code,0}, {atom,1049}, {other_system,0}]}, {alarms,[]}, {listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,6708623872}, {disk_free_limit,50000000}, {disk_free,105586227712}, {file_descriptors,[{total_limit,1048576}, {total_used,2}, {sockets_limit,943626}, {sockets_used,0}]}, {processes,[{limit,1048576},{used,57}]}, {run_queue,0}, {uptime,6}] ``` 希望这个回答能够帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值