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 }
  • 相关阅读:
    [BZOJ3745][Coci2015]Norma
    [OJ#15]TR #2 画心
    [BZOJ3585][BZOJ3339]mex
    [OJ#63]树句节够提
    [LOJ#2255][BZOJ5017][Snoi2017]炸弹
    [LOJ#525]「LibreOJ β Round #4」多项式
    【字符编码】Java字符编码详细解答及问题探讨
    【Java基础】序列化与反序列化深入分析
    【目录】JVM目录
    【知识积累】随机数生成的几种方法
  • 原文地址:https://www.cnblogs.com/speaklessdomore/p/3678189.html
Copyright © 2011-2022 走看看