亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

鏈表

標簽:
Java
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人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消