LinkedCr类
public class LinkedCr {
public int no;
public LinkedCr pre;
public LinkedCr next;
public LinkedCr(int no){
this.no = no;
}
@Override
public String toString() {
return "LinkedCr{" +
"no=" + no +
'}';
}
}
LinkedCrDemo类
public class LinkedCrDemo {
private LinkedCr head = new LinkedCr(0);
private int maxSize;
private int record = 0;
public LinkedCrDemo(int maxSize) {
this.maxSize = maxSize;
}
public Boolean isNull() {
if (head.next == null) {
return true;
} else {
return false;
}
}
public Boolean isFull() {
if (maxSize == record) {
return true;
} else {
return false;
}
}
//插入值
public void insert(LinkedCr linkedCr) {
if(isFull()){
throw new RuntimeException("链表已满");
}
LinkedCr linkedCr1 = head;
int pd = 0;
while(linkedCr1.next != null){
if(linkedCr1.next != null && linkedCr1.next.no >= linkedCr.no){
pd ++;
record ++;
LinkedCr jl = linkedCr1.next;
linkedCr1.next = linkedCr;
linkedCr.pre = linkedCr1;
linkedCr.next = jl;
break;
}
linkedCr1 = linkedCr1.next;
}
if(pd == 0){
linkedCr1.next = linkedCr;
linkedCr.pre = linkedCr1;
}
}
//打印所有
public void listAll(){
if(isFull()){
throw new RuntimeException("链表为空,无法输出");
}
LinkedCr linkedCr = head;
while(linkedCr.next != null){
System.out.println(linkedCr.next.no);
linkedCr = linkedCr.next;
}
}
}
LinkedCrDemoMain类
public class LinkedCrDemoMain {
public static void main(String[] args) {
LinkedCrDemo linkedCrDemo = new LinkedCrDemo(10);
LinkedCr linkedCr1 = new LinkedCr(21);
LinkedCr linkedCr2 = new LinkedCr(23);
LinkedCr linkedCr3 = new LinkedCr(3);
LinkedCr linkedCr4 = new LinkedCr(6);
LinkedCr linkedCr5 = new LinkedCr(12);
LinkedCr linkedCr6 = new LinkedCr(72);
linkedCrDemo.insert(linkedCr1);
linkedCrDemo.insert(linkedCr2);
linkedCrDemo.insert(linkedCr3);
linkedCrDemo.insert(linkedCr4);
linkedCrDemo.insert(linkedCr5);
linkedCrDemo.insert(linkedCr6);
linkedCrDemo.listAll();
}
}
|