继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

链表

qq_MEGALOVANIA_0
关注TA
已关注
手记 15
粉丝 7
获赞 23
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;
		}
	}
}

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP