Java实现双向链表的实现 简单的工厂代理模式

设计思想:

这里写图片描述

package ListTest;

//定义链表接口 定义链表行为的地方
interface Link {
    void add(Object obj);
    boolean remove(int index);
    boolean contains(Object obj);
    int indexOf(Object obj);
    boolean set(int index, Object obj);
    Object get(int index);
    int length();
    void clear();
    Object[] toArray();
    void printLink();
}

//工厂模式
class Factory{
    private Factory() {}
    public static Link getLinkInstance() {
        return new LinkImpl();
    }
}

//实现接口
class LinkImpl implements Link {
    private class Node {
        Object data;
        Node prev;
        Node next;
        public Node(Node prev, Object obj, Node next) {
            this.data = obj;
            this.prev = prev;
            this.next = next;
        }
    }
    private Node first;
    private Node last;
    int size;
    public void add(Object obj) {
        if(first == null) {
            Node newNode = new Node(null, obj, null);
            this.first = this.last = newNode;
            newNode.prev = newNode.next = null;
            ++this.size;
        } else{
            Node newNode = new Node(last, obj, null);
            this.last.next = newNode;
            this.last = newNode;
            ++this.size;
        }
    }
    public boolean remove(int index) {
        if(!this.isEmptyIndex(index)) {
            return false;
        }
        Node rm = (Node)this.get(index);
        if(rm == this.first) {
            if(this.first == this.last) {
                this.first = this.last = null;
                this.size--;
            } else {
                this.first = rm.next;
                this.first.prev = null;
                this.size--;
            }
        } else if (rm == this.last) {
            this.last = rm.prev;
            this.last.next = null;
            this.size--;
        } else {
            rm.prev.next = rm.next;
            rm.next.prev = rm.prev;
            this.size--;
        }
        return true;
    }

    public boolean contains(Object obj) {
        for(Node tmp = this.first; tmp != null; tmp = tmp.next) {
            if(obj.equals(tmp.data)) {
                return true;
            }
        }
        return false;
    }
    public int indexOf(Object obj) {
        int index = 0;
        for(Node tmp = this.first; tmp != null; tmp = tmp.next) {
            ++index;
            if(obj.equals(tmp.data)) {
                return index;
            }
        }
        return -1;
    }
    public boolean set(int index, Object obj) {
        if(!this.isEmptyIndex(index)) {
            return false;
        }
        Node tmp = (Node)this.get(index);
        tmp.data = obj;
        return true;
    }
    public Object get(int index) {
        if(!this.isEmptyIndex(index)) {
            return false;
        }
        Node tmp = this.first;
        while(index-- != 0) {
            tmp = tmp.next;
        }
        return tmp;
    }
    public int length() {
        return this.size;
    }
    public void clear() {
        for(Node tmp = this.first; tmp != null;) {
            Node next = tmp.next;
            tmp.next = tmp.prev = null;
            tmp = next;
        }
    }
    public Object[] toArray() {
        Object[] obj = new Object[this.size];
        int j = 0;
        for(Node i = this.first; i != null; i = i.next) {
            obj[j++] = i.data;
        }
        System.out.println("转换为数组时数组的长度:" + obj.length);
        return obj;
    }
    public void printLink() {
        for(Object data : this.toArray()) {
            System.out.println(data);
        }
    }
    public boolean isEmptyIndex(int index) {
        return index >= 0 && index <= this.size;
    }
}

//主函数,测试链表
public class ListTest {
    public static void main(String[] args) {
        Link link = Factory.getLinkInstance();
        link.add("车头");
        link.add("一号车厢");
        link.add("二号车厢");
        link.add("三号车厢");
        link.printLink();
        System.out.println("link 的长度" + link.length());
        link.remove(3);
        link.printLink();
        System.out.println("link 的长度" + link.length()); 
    }

在eclipse 4.7.2 下运行结果
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值