1. 程式人生 > >java中的鏈表編寫

java中的鏈表編寫

set 取出 ext.get ins 增加 move else size logs

通過while循環取出節點內容

class Node{//定義一個節點類,用於保存數據和取得下一個節點
    private String data;//節點中數據
    private Node next;//下一個節點
    public Node(String data){
        this.data = data;
    }
    public void setNext(Node next){
        this.next = next;
    }
    public Node getNext(){
        return next;
    }
    public
String toString(){//取出節點數據 return this.data; } } public class Test{ public static void main(String args[]){ //1.設置節點內容 Node root = new Node("根節點"); Node node1 = new Node("節點1"); Node node2 = new Node("節點2"); Node node3 = new Node("節點3");
//2.設置節點之間的關系 root.setNext(node1); node1.setNext(node2); node2.setNext(node3); //3.取出節點內容 Node currentNode = root ; //表示當前節點 while(currentNode != null){ //當前節點不為空 System.out.println(currentNode); currentNode = currentNode.getNext();//將當前節點設置為下一個節點
} } }

通過遞歸調用進行節點的取出

class Node{//定義一個節點類,用於保存數據和取得下一個節點
    private String data;//節點中數據
    private Node next;//下一個節點
    public Node(String data){
        this.data = data;
    }
    public void setNext(Node next){
        this.next = next;
    }
    public Node getNext(){
        return next;
    }
    public String toString(){//取出節點數據
        return this.data;
    }
    
}
public class Test{
    public static void main(String args[]){
        //1.設置節點內容
        Node root = new Node("根節點");
        Node node1 = new Node("節點1");
        Node node2 = new Node("節點2");
        Node node3 = new Node("節點3");
        //2.設置節點之間的關系
        root.setNext(node1);
        node1.setNext(node2);
        node2.setNext(node3);
        //3.取出節點內容,
        print(root);
    }
    //定義一個遞歸調用類
        public static void print(Node currentNode){
            if(currentNode == null){
                return;//結束調用
            }
            System.out.println(currentNode);
            print(currentNode.getNext());
        }
}

完善鏈表客戶端調用,簡化客戶端程序代碼

class Node{//定義一個節點類,用於保存數據和取得下一個節點
    private String data;//節點中數據
    private Node next;//下一個節點
    public Node(String data){
        this.data = data;
    }
    //=============================實現節點的添加操作==========================================
    public void addNode(Node newNode){
        //第一次調用addNode的當前對象 this = this.root
        //第二次調用addNode的當前對象 this = this.root.next
        if(this.next == null){ // 當前節點下一個節點不存在
            this.next = newNode;
        }else{ // 下一個節點存在
            this.next.addNode(newNode);
        }
    }
    //=============================實現節點的打印==========================================
    public void printNode(){
        System.out.println(this.data);//輸出當前數據
        if(this.next != null){
            this.next.printNode();
        }
    }
}
class Link{
    private Node root;//表示根節點
    //=============================實現節點的添加操作==========================================
    public void add(String data){
        Node newNode = new Node(data);
        if(this.root == null){//根節點不存在
            this.root = newNode; 
        }else{//表示根節點已經存在了
            this.root.addNode(newNode);
        }
    }
    //=============================實現節點的打印==========================================
    public void print(){
        if(this.root != null){
            this.root.printNode();
        }
    }
}
public class Test{
    public static void main(String args[]){
        Link link = new Link();
        link.add("根節點");
        link.add("節點1");
        link.add("節點2");
        link.add("節點3");
        link.print();
    }
}

通過內部類的方法對Node類進行封裝,只可以被Link類所利用;然後實現鏈表的增刪改查等操作;

public void add(Object obj);      數據的增加

public int size();            取得鏈表長度

public boolean isEmpty();       判斷鏈表是否為空

public void contains(Object obj);    判斷某一數據是否存在

public Object get(int index){}      根據索引取得數據

public void set(int index,Obect obj);  修改指定索引內容

public void remove(Object obj);;    刪除指定數據

publci Object [] toArray();       將鏈表以對象數組的形式返回

範例:以下是相對完整的鏈表結構

//利用內部類對鏈表進行開發
class Link{
    private class Node{
        private String data;
        private Node next;
        public Node(String data){
            this.data = data;
        }
    //==========================1.數據增加方法====================================
        public void addNode(Node newNode){
            if(this.next == null){ //1.this = this.root 2.this = this.root.next;...
                this.next = newNode;
            }else{
                this.next.addNode(newNode);    //遞歸調用
            }
        }
    //==========================4.判斷某個內容是否包含在節點之中====================================
        public boolean containsNode(String data){
            if(data.equals(this.data)){
                return true;
            }else{
                if(this.next != null){
                    return this.next.containsNode(data);
                }else{
                    return false;
                }
            }
        }
    //==========================5.根據索引取得數據====================================
        public String getNode(int index){
            if(Link.this.foot++ == index){
                return this.data;
            }else{
                return this.next.getNode(index);
            }
        }
    //==========================6.根據索引替換內容====================================
        public void setNode(int index,String data){
            if(Link.this.foot++ == index){
                this.data = data;
            }else{
                this.next.setNode(index,data);
            }
        }
    //==========================7.刪除指定的數據====================================
        public void removeNode(Node preNode,String data){//preNode表示當前節點的前一個節點
            if(data.equals(this.data)){
                preNode.next = this.next;//當前的節點的上一個
            }else{
                this.next.removeNode(this,data);
            }
        }
    //==========================8.將鏈表變為數組====================================
        public void toArrayNode(){
            Link.this.retArray[Link.this.foot++] = this.data;
            if(this.next != null){
                this.next.toArrayNode();
            }
        }
    }
    //==========================上面顯示的是內部類====================================
    private Node root;
    private int count = 0;//表示鏈表的長度
    private int foot =0;//節點的腳標
    private String[] retArray;//表示返回的數組
    //==========================1.數據增加方法====================================
    public void add(String data){
        Node newNode = new Node(data);
        if(this.root == null){        //表示根節點對象是一個空對象
            this.root = newNode;    //實例化根節點對象
        }else{    //根節點對象已經實例化
            this.root.addNode(newNode);
        }
        if(data != null){
            this.count++;//每一次調用數據count自增一次
        }
    }
    //==========================2.取得鏈表長度====================================
    public int size(){
        return count;
    }
    //==========================3.判斷鏈表是否是空鏈表====================================
    public boolean isEmpty(){
        return this.count==0;
    }
    //==========================4.判斷某個內容是否包含在節點之中====================================
    public boolean contains(String data){
        if(this.root == null||data == null){
            return false;
        }else{
            return this.root.containsNode(data);
        }
    }
    //==========================5.根據索引取得數據====================================
    public String get(int index){
        this.foot = 0;
        if(this.count <= index){
            return null;
        }else{
            return this.root.getNode(index);
        }
    }
    //==========================6.根據索引替換內容====================================
    public void set(int index,String data){
        this.foot = 0;//重新設置腳標內容
        if(this.count  <= index){
            return ;//結束方法的調用
        }else{
            this.root.setNode(index,data);
        }
    }
    //==========================7.刪除指定的數據====================================
    public void remove(String data){
        if(this.contains(data)){ //要刪除的數據包含在數據裏面
            if(this.root.data.equals(data)){// 刪除的是根節點數據
                //根節點要變為原來根節點的下一個節點;
                this.root = this.root.next;
            }else{ //要刪除的不是根節點
                this.root.next.removeNode(this.root,data);
            }
            this.count--;
        }
    }
    //==========================8.將鏈表變為數組====================================
    public String [] toArray(){
        if(this.root == null){
            return null;
        }else{
            this.foot = 0;
            this.retArray = new String[this.count];
            this.root.toArrayNode();
            return this.retArray;
        }
    }
}
public class Test{
    public static void main(String args[]){
        Link link = new Link();
        System.out.println(link.isEmpty());
        link.add("根節點");
        link.add("節點1");
        link.add("節點2");
        link.add("節點3");
        /*
        System.out.println(link.isEmpty());
        System.out.println(link.size());
        System.out.println(link.contains("節點"));
        System.out.println(link.get(3));
        link.set(1,"修改節點內容");
        System.out.println(link.get(1));    //根節點
        */
        /*
        System.out.println(link.get(1));
        System.out.println(link.size());
        link.remove("節點1");
        System.out.println(link.get(1));
        System.out.println(link.size());
        */
        String data[] = link.toArray();
        for(int x=0;x<data.length;x++){
            System.out.print(data[x]+"\t");
        }
        
    }
}

java中的鏈表編寫