java-链表详解

##java实现链表的主要操作

interface ILink{
    boolean add(Object data);
    void  printLink();
    Object remove(Object data);
    Object set(int index,Object data);
    Object get(int index);
    boolean contains(Object data);
    Object[] toAttary();
    int size();
    boolean clean();
}
class LinkImpl implements ILink{
    private Node head;
    private Node last;
    private int size;
    class Node{
        Object data;
        Node next;
        Node prov;
        public Node(Object data, Node next, Node prov) {
            this.data = data;
            this.next = next;
            this.prov = prov;
        }
    }
    public boolean add(Object data) {
        Node temp = this.last;
        Node newnode = new Node(data,null,temp);
        this.last = newnode;
        if(this.head == null){
            this.head = newnode;
        }
        else{
            temp.next = newnode;
        }
        this.size++;
        return true;
    }
    @Override
    public void printLink() {
        Object[] obj = toAttary();
        if(obj == null)return;
        for(Object i:obj){
            System.out.print(i+"  ");
        }
        System.out.println();
    }
    private Object removeNode(Node node){
        Node prov = node.prov;
        Object elementData = node.data;
        Node next = node.next;
        if(node.prov == null){
            this.head = next;
        }else{
            prov.next = next;
            node.prov= null;
        }
        if(node.next == null){
            this.last = prov;
        }else{
            next.prov = prov;
            node.next = null;
        }
        node.data = null;
        return elementData;

    }
    public Object remove(Object data) {
        for(Node temp = this.head;temp != null;temp= temp.next){
            if(temp.data ==data ||
                    ( data != null && data.equals(temp.data))){
                this.size--;
                return removeNode(temp);

            }
        }
        return null;
    }
    private  Node find(int index){
        if(index<(size>>1)) {
            Node temp = this.head;
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
            return temp;
        }
        else{
            Node temp = this.last;
            for(int i=size-1;i>index;i--){
                temp = temp.prov;
            }
            return temp;
        }
    }
    public Object set(int index,Object data) {
        if(index<0 && index >size-1){
            return null;
        }
        Node node = find(index);
        Object elementData = node.data;
        node.data = data;
        return elementData;
    }
    public Object get(int index) {
        if(index<0 && index >size-1){
            return null;
        }
        return find(index).data;
    }
    public boolean contains(Object data) {
        for(Node temp = this.head;temp != null;temp = temp.next){
            if(data == temp.data ||
                    (data !=null && data.equals(temp.data))){
                return true;
            }
        }
        return false;
    }
    public int size() {
        return size;
    }
    public boolean clean() {
        for(Node temp = this.head;temp != null;){
            Node node = temp.next;
            temp.next =temp.prov = null;
            temp.data = null;
            temp =node;
            size--;
        }
        return true;
    }
    public Object[] toAttary(){
        if(size == 0){
            return null;
        }
        Object[] obj = new Object[size];
        int i=0;
        for(Node temp = this.head;temp != null;temp=temp.next){
            obj[i++] = temp.data;
        }
        return obj;
    }
}
public class Test{
    public static void main(String[] args) {
        ILink link = new LinkImpl();
        link.add("火车头");
        link.add("火车箱1");
        link.add(null);
        link.add("火车箱2");
        link.add("火车箱3");
        link.add("火车尾");
        link.printLink();
        System.out.println(link.contains("火车头"));
        System.out.println(link.contains("火车"));
        link.remove("火车头");
        link.remove("火车箱2");
        link.remove("pangpang") ;
        link.remove(null);
        link.set(1,"pangpang");
        link.printLink();
        link.clean();
        link.printLink();
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页