zoukankan      html  css  js  c++  java
  • java实现有序链表

     1 package com.liu.Link;
     2 //测试类
     3 class SortedListApp
     4 {
     5     public static void main(String args[])
     6     {
     7         SortedList theSortedList = new SortedList();
     8         theSortedList.insert(40);
     9         theSortedList.insert(20);
    10         theSortedList.displayList();
    11         
    12         theSortedList.insert(10);
    13         theSortedList.insert(15);
    14         theSortedList.insert(50);
    15         theSortedList.displayList();
    16         
    17         theSortedList.remove();
    18         theSortedList.displayList();
    19         
    20     }
    21 }
    22 
    23 
    24 
    25 
    26 
    27 //有序链表实现类
    28 public class SortedList {
    29     private Link5 first;
    30     public SortedList(){
    31         first = null;
    32     }
    33     public boolean isEmpty()
    34     {
    35         return first==null;
    36     }
    37     public void insert(long d){
    38         Link5 newLink = new Link5(d);
    39         Link5 previous = null;
    40         Link5 current = first;
    41         
    42         while(current!=null&&d>current.dData){
    43             previous = current;
    44             current = current.next;
    45         }
    46         //当有序链表是一个空表时
    47         if(previous == null)
    48         {
    49             first = newLink;
    50         }else
    51         {
    52             previous.next = newLink;
    53         }
    54         newLink.next = current;
    55     }
    56     
    57     public Link5 remove()
    58     {
    59         Link5 temp = first;
    60         first = first.next;
    61         return temp;
    62     }
    63     
    64     public void displayList()
    65     {
    66         System.out.print("List (first-->last):");
    67         Link5 current = first;
    68         while(current!=null)
    69         {
    70             current.displayLink();
    71             current = current.next;
    72         }
    73         System.out.println("");
    74     }
    75 }
    76 //有序链表数据类
    77 class Link5
    78 {
    79     public long dData;
    80     public Link5 next;
    81     public Link5(long d){
    82         dData = d;
    83     }
    84     public void displayLink(){
    85         System.out.print(dData+" ");
    86     }
    87 }
  • 相关阅读:
    Eureka与Zookeeper的区别
    Eureka的集群配置
    Rest微服务案例
    Eureka概述
    SpringCloud与Dubbo区别对比
    各种微服务框架对比
    ElasticSearch(九):elasticsearch-head插件安装
    ElasticSearch(八):elasticsearch.yml配置说明
    Docker(2):使用Dockerfile创建支持SSH服务的镜像
    CentOS 7 安装Kubernetes(单机版)
  • 原文地址:https://www.cnblogs.com/speaklessdomore/p/3678189.html
Copyright © 2011-2022 走看看