class Node {
public int date;
public Node next;
public Node(int date){
this.date=date;
}
}
public class ListNode {
public Node head;
public ListNode(Node head){
this.head = head;
}
public int length(){
int i = 0;
Node temp = head;
while(temp!=null){
temp = temp.next;
i++;
}
return i;
}
public void print(){
Node temp = head;
while(temp!=null){
System.out.print(temp.date+" ");
temp = temp.next;
}
System.out.println();
}
public void addNode(Node node){
Node temp = head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
}
public void insertNodeByIndex(int index,Node node){
int i = 1;
Node temp = head;
while(i!=index-1){
temp = temp.next;
i++;
}
node.next = temp.next;
temp.next = node;
}
public void deleteNodeByIndex(int index){
int i = 1;
Node temp = head;
while(i!=index-1){
temp = temp.next;
i++;
}
temp.next = temp.next.next;
}
public void updateNodeByIndex(int index,int date){
int i = 1;
Node temp = head;
while(i!=index){
temp = temp.next;
i++;
}
temp.date = date;
}
public Node findNodeByIndex(int index){
int i = 1;
Node temp = head;
while(i!=index){
temp = temp.next;
i++;
}
return temp;
}
public void sort2(ListNode ln){
Node temp = head;
while(temp!=null){
Node temp2 = temp.next;
while(temp2!=null){
if(temp2.date<temp.date){
int date = temp.date;
temp.date = temp2.date;
temp2.date = date;
}
temp2 = temp2.next;
}
temp = temp.next;
}
}
}
打开App,阅读手记