链表的具体实现

 数据本身不具备链接的操作,要想实现链表处理,需要有一个公共的结构,即数据的保存+下一个的指向。

package linkedDemo;
// 这种方式如果直接操作Node,很麻烦
class Node<E> {
    private E name;
    private Node<E> next;

    public void setName(E name) {
        this.name = name;
    }

    public E getName() {
        return this.name;
    }

    public Node(E name) {
        this.name = name;
    }

    public Node getNext() {
        return this.next;
    }

    public void setNext(Node<E> next){
        this.next = next;
    }
}

public class NodeDemo {
    public static void main(String[] args) {
        Node<String> n0 = new Node<>("火车头");
        Node<String> n1 = new Node<>("车厢一");
        Node<String> n2 = new Node<>("车厢二");
        Node<String> n3 = new Node<>("车厢三");
        Node<String> n4 = new Node<>("车厢四");
        Node<String> n5 = new Node<>("车厢五");
        n0.setNext(n1);
        n1.setNext(n2);
        n2.setNext(n3);
        n3.setNext(n4);
        n4.setNext(n5);
        n5.setNext(null);
        print(n0);
    }

    public static void print(Node<?> node){
        if(node != null) {
            System.out.println( node.getName());
            print(node.getNext());
        }
    }
}

优化逻辑如下:

package linkedDemo;
interface ILink<E>{
    public void add(E e);
    public int getSize();
    public boolean isEmpty();
    public void delete(E e);
    public Object[] toArray();
    public E get(int index);
    public void set(int index, E data);
    public boolean isExistData(E e);
    public void clean();
}

class LinkImpl<E> implements ILink<E>{

    class Node<E>{
        private E data;
        private Node next;
        public Node(E data){
            this.data = data;
        }


        public void delete(Node previous,  E data){
            if(this.data.equals(data)){
               previous.next = this.next;
            }else{
                if(this.next != null){
                    this.next.delete(this, data);
                }
            }


        }

        public void addNode(Node node){
            if(this.next == null){
                this.next = node;
            }else{
                this.next.addNode(node);
            }
        }

        public void set(int index, E data){
            if(index == LinkImpl.this.foot++){
                this.data = data;
            }else{
                this.next.set(index,data);
            }

        }

        public void toArrayNode(){
            LinkImpl.this.returunResut[LinkImpl.this.foot++] = this.data;
            if(this.next == null){
                return;
            }else{
                this.next.toArrayNode();
            }
        }

        public E get(int index){
            if(index == LinkImpl.this.foot++){
                return this.data;
            }else if(index > LinkImpl.this.count -1){
                return null;
            }else {
               return (E)this.next.get(index);
            }
        }

        public boolean isExistData(E e){
            if(this.next == null){
                return false;
            }else if(this.next.data.equals(e)){
                return true;
            }else{
                return this.next.isExistData(e);
            }
        }
    }

    // LinkImpl自身相关内容
    private Node<E> root;
    private int count;
    private int foot;
    public Object[] returunResut;

    @Override
    public Object[] toArray(){
        if(this.root==null){
            return null;
        }else{
            this.foot = 0;
            this.returunResut = new Object[this.count];
            this.root.toArrayNode();
            return this.returunResut;
        }
    }

    @Override
    public boolean isEmpty(){
//        return this.count==0; 两种方式都可以
        return this.root ==  null;
    };

    @Override
    public void set(int index, E data){
        this.foot = 0;
        if(this.root == null ){
            return ;
        }
        this.root.set(index, data);
    }


    @Override
    public int getSize(){
        return this.count;
    }
    @Override
    public void add(E e){
        if(e == null){
            return;
        }
       Node newNode =  new Node(e);
        if(this.root == null){
            this.root = newNode;
        }else{
            this.root.addNode(newNode);
        }
        this.count ++;
    }


    @Override
    public void clean(){
        this.root = null;
        this.count = 0;
    }

    @Override
    public boolean isExistData(E e){
        if(this.root == null){
            return false;
        }
        return this.root.isExistData(e);
    }


    @Override
    public void delete(E e){
        if(this.root == null || e == null){
            return;
        }
        if(this.root.isExistData(e)){
            if(this.root.data.equals(e)){
                this.root = this.root.next;
            }else{
                this.root.next.delete(this.root,e);
            }
        }
        this.count--;
    }

    @Override
    public E get(int index){
        this.foot = 0 ;
       if(this.root == null || index > this.count -1){
           return null;
       }else {
           return this.root.get(index);
       }
    }


}
public class JavaDemo {
    public static void main(String[] args) {
        LinkImpl<String> link = new LinkImpl<>();
//        System.out.println(link.getSize());
//
//        System.out.println(link.isEmpty());
        link.add("hello");
        link.add("mldn");
        link.add("com");

//        Object[] obj = link.toArray();
//        for (Object value: obj) {
//            System.out.print(value);
//        }
//        System.out.println(link.getSize());
//        System.out.println(link.isEmpty());
        link.set(2,"yes");

        System.out.println(link.get(2));
        System.out.println(link.isExistData("yes"));
        System.out.println(link.isExistData("yes1"));
        link.delete("yes");
        System.out.println(link.isExistData("yes"));
        link.clean();
        System.out.println(link.getSize());




//        LinkImpl.Node n = link.find("hello");
//        System.out.println(n);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值