java 有序数据结构_Java数据结构——有序链表

8850a26c45cf95184d8a03a1bb0ac437.png

ff83a31ebd965a7a57c213d7095fcdf9.png

//=================================================

// File Name :SortedList_demo

//------------------------------------------------------------------------------

// Author :Common

//类名:SortedList

//属性:

//方法:

class SortedList{

private Link_long first;

public SortedList(){//构造函数

first = null;

}

public void insert(long key){

Link_long newLink = new Link_long(key);

Link_long previous = null;//上一次插入的值

Link_long current = first;//每插入一次,current就重新赋为表头的值

while(current != null && key > current.dData){//没进入这里,pre就是null,也就只进入下面if的上一层

previous = current;

current = current.next;//current的位置往后移动

}

if(previous == null){//最开始的情况,给first赋值为newLink,即key

first = newLink;

}else{//

previous.next = newLink;

}

newLink.next = current;

}

public long remove(){

Link_long temp = first;

first = first.next;

return temp.dData;

}

public void displayList(){

System.out.println("List (first-->last)");

Link_long current = first;

while(current != null){

current.displayLink();

current = current.next;

}

}

}

//主类

//Function : SortedList_demo

public class SortedList_demo {

public static void main(String[] args) {

// TODO 自动生成的方法存根

SortedList theSortedList = new SortedList();

theSortedList.insert(10);

theSortedList.insert(20);

theSortedList.insert(30);

theSortedList.displayList();

theSortedList.remove();

theSortedList.remove();

theSortedList.displayList();

}

}

f7e68c8a463db5951ced864f5bc83a16.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值