補充3:單鏈表的實現

鏈表類

	public class Node{
		int data;
		Node next;
		public Node(int data){
			this.data = data;
		}
	}

1.插入元素創建鏈表

    當一個鏈表的最後一個節點的next爲null時即爲鏈表的末尾節點,現在末尾節點後繼續添加節點,先將新節點放在最後,然後修改上一個末尾節點的next。

	public  void add(int data){
		Node node = new Node(data);
		if(head==null){
			head = node;
		}else{
			last.next = node;
		}
		last = node;
	}

2.在指定位置插入節點

    先找到要插入的位置的前一節點,並將前一節點的next指向新節點,新節點的next則指向前一節點之前的next

	public void insert(int location,int data){
		Node node = head;
		int i = 0;
		while(node!=null&&i<location-2){
			node = node.next;
			i++;
		}
		Node newnode = new Node(data);
		newnode.next = node.next;
		node.next = newnode;
	}

3.刪除指定位置的節點

    類似於插入指定節點,先找出要刪除的節點位置的前一節點,將next改爲next的next

	public void delete(int location){
		Node node = head;
		int i=0;
		while(node!=null&&i<location-2){
			node = node.next;
			i++;
		}
		node.next = node.next.next;
	}

4.修改節點數據

    找出該節點位置,修改數值即可

	public void modify(int location,int data){
		Node node = head;
		int i=0;
		while(node!=null&&i<location-1){
			node = node.next;
			i++;
		}
		node.data = data;
	}

5.打印鏈表

	public void print(){
		Node node = head;
		while(node!=null){
			System.out.print(node.data);
			node = node.next;
		}
	}

6.鏈表長度

	public int length(){
		int length = 0;
		Node node = head;
		while(node!=null){
			length++;
			node = node.next;
		}
		return length;
	}

7.查找指定位置節點

public Node get(int location){  
    Node node=head;  
    int i=1;  
    while(node!=null&&i<location){  
        node=node.next;  
        i++;  
    }  
    return node;  
}

完整測試

public class javatest {
		Node head =null;
		Node last =null;	
	
	public class Node{
		int data;
		Node next;
		public Node(int data){
			this.data = data;
		}
	}
	
	public  void add(int data){
		Node node = new Node(data);
		if(head==null){
			head = node;
		}else{
			last.next = node;
		}
		last = node;
	}
	
	public void insert(int location,int data){
		Node node = head;
		int i = 0;
		while(node!=null&&i<location-2){
			node = node.next;
			i++;
		}
		Node newnode = new Node(data);
		newnode.next = node.next;
		node.next = newnode;
	}
	
	public void delete(int location){
		Node node = head;
		int i=0;
		while(node!=null&&i<location-2){
			node = node.next;
			i++;
		}
		node.next = node.next.next;
	}
	
	public void modify(int location,int data){
		Node node = head;
		int i=0;
		while(node!=null&&i<location-1){
			node = node.next;
			i++;
		}
		node.data = data;
	}
	
	public void print(){
		Node node = head;
		while(node!=null){
			System.out.print(node.data);
			node = node.next;
		}
	}
	
	public int length(){
		int length = 0;
		Node node = head;
		while(node!=null){
			length++;
			node = node.next;
		}
		return length;
	}
	
	
	public static void main(String[] args){
		javatest link = new javatest();
		link.add(1);
		link.add(2);
		link.add(3);
		link.add(4);
		link.add(5);
		link.add(6);
		link.add(7);
		link.add(8);
		link.add(9);
		link.add(10);
		System.out.println(link.length());
		System.out.println("head.data:" + link.head.data);
		link.print();
		System.out.println();
		link.modify(1, 888);
		System.out.println("after modipfy:" );
		link.print();
		System.out.println();
		link.delete(6);
		System.out.println("after delete:" );
		link.print();
		
	}
}

輸出結果:

10
head.data:1
12345678910
after modipfy:
8882345678910
after delete:
888234578910

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章