码迷,mamicode.com
首页 > 编程语言 > 详细

java中的链表编写

时间:2017-08-09 10:05:29      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:数组   this   增删改查   array   二次   删除   内部类   开发   lin   

通过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中的链表编写

标签:数组   this   增删改查   array   二次   删除   内部类   开发   lin   

原文地址:http://www.cnblogs.com/hu1056043921/p/7307353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!