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){
//temp指向待排序位置的节点
Node temp = head;
while(temp!=null){
//temp2指向temp的后面的节点
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;
}
}
}
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦