您的位置:首页 > 编程语言 > Java开发

Java链表的基本操作

2018-01-29 20:19 267 查看
class Link    {

    private int count = 0;    //节点个数

    private Node root;    //头节点引用

    private int foot;    //脚标

    private Object[] retData;

//--------------------------------------------------------Node类--------------------------------------------------------    

    private class Node    {

        private Object data;    //数据域

        private Node next;    //指针域

        public Node(Object data){

            this.data = data;

        }

        // 插入节点

        public void addNode(Node newNode)    {    //把新Node对象和节点引用对接

            if(this.next == null){        //如果在当前指针为null,就指向新Node对象

                this.next = newNode;

            }else {    //如果当前节点有指向,就用下一个节点再调用创建节点方法,直到链表末尾

                this.next.addNode(newNode);

            }

        }

        //转换数组

        public void toArrayNode()    {

            Link.this.retData[Link.this.foot++] = this.data;    //this为当前对象Node(root),Link.this为当前对象Link(list)

            if(this.next != null)    {

                this.next.toArrayNode();

            }

        }

        //数据查询

        public boolean containsNode(Object search)    {

            if(search.equals(this.data)){    //如果找到输出ture

                return true;

            }else    {

                if(this.next != null)    {    //没有找到继续下一个

                    return this.next.containsNode(search);

                }else    {    //最终没有找到输出false

                    return false;

                }

            }

        }

        //    索引查询

        public Object getNode(int index)    {

            if(Link.this.foot++ == index)    {    //找到返回值

                return this.data;    

            }else    {

                return this.next.getNode(index);    //没有找到下一个

            }

        }

        //更改索引

        public void setNode(int index,Object newData)    {

            if(Link.this.foot++ == index)    {

                this.data = newData;

            }else    {

                if(this.next != null)    {

                    this.next.setNode(index,newData);

                }

            }

        }

        //删除非根数据

        public void removeNode(Node previous,Object data)    {

            if(this.data.equals(data))    {    //找到数据删除

                previous.next = this.next;

                System.out.println("删除数据成功!");

            }else    {

                this.next.removeNode(this,data);    //把当前的对象作为下一个对象调用本发方法的上一个对象参数

            }

        }

    }

//-------------------------------------------------LINK方法------------------------------------------------------

        //调用插入节点方法

        public void add(Object data)    {

            if(data == null){    //如果没有传入数据域,链表不操作

                return ;

            }

            Node newNode = new Node(data);    //对创建节点的动作进行封装

            if(this.root == null){

                root = newNode;    //设置头节点

            }else{    //链表中已经有数据

                this.root.addNode(newNode);    //从节点头开始遍历把Node对象放入链表末端

            }

            this.count++;    //每调用一次插入函数,计数加一

        }

        //计算链表节点个数

        public int size()    {

            return this.count;

        }

        //调用转换数组方法    

        public Object[] toArray()    {

            if(this.count == 0)    {return null;}    //    空链表

            this.retData = new Object[this.count];    //初始化数组

            this.foot = 0;

            this.root.toArrayNode();

            return this.retData;

        }

        //调用数据查询方法

        public boolean contains(Object search){    //对比参数

            if(search == null || this.root == null){return false;}

            return this.root.containsNode(search);

        }

        //调用索引查询方法

        public Object get(int index)    {

            if(this.root == null || index >= this.count)    {

                return null;

            }else{

                this.foot = 0;    //初始化

                return this.root.getNode(index);    //从头节点开始匹配

            }

        }

        //调用更改索引方法

        public void set(int index,Object newData)    {

            if(this.root == null || index >=this.count )    {

                System.out.println("空链表或超出范围");

            }else{

                this.foot = 0;

                this.root.setNode(index,newData);

                System.out.println("更改成功!");

            }

        }

        //删除根数据

        public void remove(Object 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 --;    //每调用一次删除函数,计数减一

            }

        }

}

public class TestDemo{

    public static void main(String args[])    {

        Link list = new Link();

        list.add("Hello");    //添加数据

        list.add("World");

        list.add("!");

        System.out.println(list.size());    //查询链表“长度”

        Object[] result = list.toArray();    //转换数组

        for(int x = 0;x<result.length;x++){

            System.out.println(result[x]);

        }

        System.out.println(list.contains("!"));    //匹配数据

        System.out.println(list.get(1));    //匹配坐标

        list.set(3,".");    //指定索引数据更改

        list.remove("!");    //删除指定对象

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: