链表的定义和使用java版

1.链表实现简介

package com.forcoldplay.test2;

class Node<E> {
    private E data;
    private Node next;
    public Node(E data) {
        this.data = data ;
    }
    public E getDate() {
        return this.data ;
    }
    public void setNext(Node<E> next) {
        this.next = next ;
    }
    public Node getNext() {
        return this.next ;
    }
}

public class LinkDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Node<String> n1 = new Node<String>("火车头");
        Node<String> n2 = new Node<String>("车厢1");
        Node<String> n3 = new Node<String>("车厢2");
        Node<String> n4 = new Node<String>("车厢3");
        Node<String> n5 = new Node<String>("车厢4");
        n1.setNext(n2);
        n2.setNext(n3);
        n3.setNext(n4);
        n4.setNext(n5);
        print(n1);
    }
    public static void print(Node<?> node) {
        if(node!=null) {
            System.out.println(node.getDate());
            print(node.getNext());
        }
    }

}

输出:

火车头
车厢1
车厢2
车厢3
车厢4

2.数据增加

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
    }

    private Node root;     //    保存的根元素
    
    @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);
        }
    }
    
    public Node getRoot() {
        return root;
    }
}

public class LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
    }
    

    
}

3.获取集合个数

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
    public int size();
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
    }

    private Node root;     //    保存的根元素
    private int 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 int size() {
        return this.count;
    }
}

public class LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        System.out.println(link.size());
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
        System.out.println(link.size());
    }
    

    
}
0
5

4.空集合判断

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
    }

    private Node root;     //    保存的根元素
    private int 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 int size() {
        return this.count;
    }


    @Override
    public boolean isEmpty() {
        if(this.count==0) {
            return true;
        }
        return false;
    }
}

public class LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        System.out.println(link.size());
        if(link.isEmpty()) {
            System.out.println("链表为空");
        }
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
        System.out.println(link.size());
        if(!link.isEmpty()) {
            System.out.println("链表为非空");
        }
    }
    

    
}
0
链表为空
5
链表为非空

5.返回集合数据

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
    public Object[] toArray();
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
        public void toArrayNode() {
            LinkImpl.this.returnDate [LinkImpl.this.foot++] = this.data;
            if( this.next != null ) {
                this.next.toArrayNode();
            }
        }
    }

    private Node root;     //    保存的根元素
    private int count;    //     保存数据个数
    private int foot;
    private Object[] returnDate;
    @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 int size() {
        return this.count;
    }


    @Override
    public boolean isEmpty() {
        if(this.count==0) {
            return true;
        }
        return false;
    }


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

}

public class LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        System.out.println(link.size());
        if(link.isEmpty()) {
            System.out.println("链表为空");
        }
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
        System.out.println(link.size());
        if(!link.isEmpty()) {
            System.out.println("链表为非空");
        }
        
        Object result[] = link.toArray();
        for(Object obj:result) {
            System.out.println(obj);
        }
    }
    
}

6.根据索引取得数据

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
    public Object[] toArray();
    public E get(int index);
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
        public void toArrayNode() {
            LinkImpl.this.returnDate [LinkImpl.this.foot++] = this.data;
            if( this.next != null ) {
                this.next.toArrayNode();
            }
        }
        
        public E getNode(int index){
            if(LinkImpl.this.foot ++ == index) {
                return this.data;
            } else {
                return this.next.getNode(index);
            }
        }
    }

    private Node root;     //    保存的根元素
    private int count;    //     保存数据个数
    private int foot;
    private Object[] returnDate;
    @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 int size() {
        return this.count;
    }


    @Override
    public boolean isEmpty() {
        if(this.count==0) {
            return true;
        }
        return false;
    }


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


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

}

public class LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        System.out.println(link.size());
        if(link.isEmpty()) {
            System.out.println("链表为空");
        }
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
        System.out.println(link.size());
        if(!link.isEmpty()) {
            System.out.println("链表为非空");
        }
        
        Object result[] = link.toArray();
        for(Object obj:result) {
            System.out.println(obj);
        }
        System.out.println(link.get(1));
        System.out.println(link.get(10));
    }
    
}

7.修改指定索引数据

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
    public Object[] toArray();
    public E get(int index);
    public void set(int dex,E data);
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
        public void toArrayNode() {
            LinkImpl.this.returnDate [LinkImpl.this.foot++] = this.data;
            if( this.next != null ) {
                this.next.toArrayNode();
            }
        }
        
        public E getNode(int index){
            if(LinkImpl.this.foot ++ == index) {
                return this.data;
            } else {
                return this.next.getNode(index);
            }
        }
        public void setNode(int index,E date) {
            if(LinkImpl.this.foot ++ == index) {
                this.data = date;
            } else {
                this.next.setNode(index,date);
            }            
        }
    }

    private Node root;     //    保存的根元素
    private int count;    //     保存数据个数
    private int foot;
    private Object[] returnDate;
    @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 int size() {
        return this.count;
    }


    @Override
    public boolean isEmpty() {
        if(this.count==0) {
            return true;
        }
        return false;
    }


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


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


    @Override
    public void set(int index, E date) {
        if(index >= this.count) {
            return ;
        }
        this.foot = 0;
        this.root.setNode(index, date);
        
    }

}

public class LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        System.out.println(link.size());
        if(link.isEmpty()) {
            System.out.println("链表为空");
        }
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
        System.out.println(link.size());
        if(!link.isEmpty()) {
            System.out.println("链表为非空");
        }
        
        link.set(1, "your");
        Object result[] = link.toArray();
        for(Object obj:result) {
            System.out.println(obj);
        }
        System.out.println(link.get(1));
        System.out.println(link.get(10));
        //System.out.println();
    }
    
}

8.判断数据是否存在

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
    public Object[] toArray();
    public E get(int index);
    public void set(int dex,E data);
    public boolean contains(E data);
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
        public void toArrayNode() {
            LinkImpl.this.returnDate [LinkImpl.this.foot++] = this.data;
            if( this.next != null ) {
                this.next.toArrayNode();
            }
        }
        
        public E getNode(int index){
            if(LinkImpl.this.foot ++ == index) {
                return this.data;
            } else {
                return this.next.getNode(index);
            }
        }
        public void setNode(int index,E date) {
            if(LinkImpl.this.foot ++ == index) {
                this.data = date;
            } else {
                this.next.setNode(index,date);
            }            
        }
        public boolean containsNode(E data) {
            if(this.data.equals(data)) {
                return true;
            } else {
                if(this.next == null) {
                    return false;
                } else {
                    return this.next.containsNode(data);
                }
            }
        }
    }

    private Node root;     //    保存的根元素
    private int count;    //     保存数据个数
    private int foot;
    private Object[] returnDate;
    @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 int size() {
        return this.count;
    }


    @Override
    public boolean isEmpty() {
        if(this.count==0) {
            return true;
        }
        return false;
    }


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


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


    @Override
    public void set(int index, E date) {
        if(index >= this.count) {
            return ;
        }
        this.foot = 0;
        this.root.setNode(index, date);
        
    }


    @Override
    public boolean contains(E data) {
        if(data==null) {
            return false;
        }
        return this.root.containsNode(data);
    }

}

public class LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        System.out.println(link.size());
        if(link.isEmpty()) {
            System.out.println("链表为空");
        }
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
        System.out.println(link.size());
        if(!link.isEmpty()) {
            System.out.println("链表为非空");
        }
        
        link.set(1, "your");
        Object result[] = link.toArray();
        for(Object obj:result) {
            System.out.println(obj);
        }
        System.out.println(link.get(1));
        System.out.println(link.get(10));
        //System.out.println();
        if(link.contains("name")) {
            System.out.println("找到了name");
        }
        if(!link.contains("seek")) {
            System.out.println("没找到seek");
        }
    }
    
}

9.数据删除

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
    public Object[] toArray();
    public E get(int index);
    public void set(int dex,E data);
    public boolean contains(E data);
    public void remove(E data);
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
        public void toArrayNode() {
            LinkImpl.this.returnDate [LinkImpl.this.foot++] = this.data;
            if( this.next != null ) {
                this.next.toArrayNode();
            }
        }
        
        public E getNode(int index){
            if(LinkImpl.this.foot ++ == index) {
                return this.data;
            } else {
                return this.next.getNode(index);
            }
        }
        public void setNode(int index,E date) {
            if(LinkImpl.this.foot ++ == index) {
                this.data = date;
            } else {
                this.next.setNode(index,date);
            }            
        }
        public boolean containsNode(E data) {
            if(this.data.equals(data)) {
                return true;
            } else {
                if(this.next == null) {
                    return false;
                } else {
                    return this.next.containsNode(data);
                }
            }
        }
        public void removeNode(Node previous,E data) {
            if(this.data.equals(data)) {
                previous.next = this.next;
            } else {
                if(this.next != null) {
                    this.next.removeNode(this, data);
                }
            }
        }
    }

    private Node root;     //    保存的根元素
    private int count;    //     保存数据个数
    private int foot;
    private Object[] returnDate;
    @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 int size() {
        return this.count;
    }


    @Override
    public boolean isEmpty() {
        if(this.count==0) {
            return true;
        }
        return false;
    }


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


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


    @Override
    public void set(int index, E date) {
        if(index >= this.count) {
            return ;
        }
        this.foot = 0;
        this.root.setNode(index, date);
        
    }


    @Override
    public boolean contains(E data) {
        if(data==null) {
            return false;
        }
        return this.root.containsNode(data);
    }


    @Override
    public void remove(E 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 LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        System.out.println(link.size());
        if(link.isEmpty()) {
            System.out.println("链表为空");
        }
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
        System.out.println(link.size());
        if(!link.isEmpty()) {
            System.out.println("链表为非空");
        }
        
        link.set(1, "your");
        link.remove("hello");
        link.remove("is");
        Object result[] = link.toArray();
        for(Object obj:result) {
            System.out.println(obj);
        }
        System.out.println(link.get(1));
        System.out.println(link.get(10));
        //System.out.println();
        if(link.contains("name")) {
            System.out.println("找到了name");
        }
        if(!link.contains("seek")) {
            System.out.println("没找到seek");
        }
    }
    
}

10.清空链表

package com.forcoldplay.test2;

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
    public Object[] toArray();
    public E get(int index);
    public void set(int dex,E data);
    public boolean contains(E data);
    public void remove(E data);
    public void clean();
}
class LinkImpl<E> implements ILink<E> {
    
    private class Node {
        private E data ;    //保存节点的数据关系
        private Node next ;    //保存的数据
        public Node (E data) {
            this.data = data;
        }
        public void addNode(Node newNode) {    //保存新的Node数据
            if(this.next==null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
        public void toArrayNode() {
            LinkImpl.this.returnDate [LinkImpl.this.foot++] = this.data;
            if( this.next != null ) {
                this.next.toArrayNode();
            }
        }
        
        public E getNode(int index){
            if(LinkImpl.this.foot ++ == index) {
                return this.data;
            } else {
                return this.next.getNode(index);
            }
        }
        public void setNode(int index,E date) {
            if(LinkImpl.this.foot ++ == index) {
                this.data = date;
            } else {
                this.next.setNode(index,date);
            }            
        }
        public boolean containsNode(E data) {
            if(this.data.equals(data)) {
                return true;
            } else {
                if(this.next == null) {
                    return false;
                } else {
                    return this.next.containsNode(data);
                }
            }
        }
        public void removeNode(Node previous,E data) {
            if(this.data.equals(data)) {
                previous.next = this.next;
            } else {
                if(this.next != null) {
                    this.next.removeNode(this, data);
                }
            }
        }
    }

    private Node root;     //    保存的根元素
    private int count;    //     保存数据个数
    private int foot;
    private Object[] returnDate;
    @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 int size() {
        return this.count;
    }


    @Override
    public boolean isEmpty() {
        if(this.count==0) {
            return true;
        }
        return false;
    }


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


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


    @Override
    public void set(int index, E date) {
        if(index >= this.count) {
            return ;
        }
        this.foot = 0;
        this.root.setNode(index, date);
        
    }


    @Override
    public boolean contains(E data) {
        if(data==null) {
            return false;
        }
        return this.root.containsNode(data);
    }


    @Override
    public void remove(E 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--;
        }
    }


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

}

public class LinkDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ILink<String> link = new LinkImpl();
        System.out.println(link.size());
        if(link.isEmpty()) {
            System.out.println("链表为空");
        }
        link.add("hello");
        link.add("my");
        link.add("name");
        link.add("is");
        link.add("tom");
        System.out.println(link.size());
        if(!link.isEmpty()) {
            System.out.println("链表为非空");
        }
        
        link.set(1, "your");
        link.remove("hello");
        link.remove("is");
        Object result[] = link.toArray();
        for(Object obj:result) {
            System.out.println(obj);
        }
        System.out.println(link.get(1));
        System.out.println(link.get(10));
        //System.out.println();
        if(link.contains("name")) {
            System.out.println("找到了name");
        }
        if(!link.contains("seek")) {
            System.out.println("没找到seek");
        }
        
        link.clean();
        System.out.println(link.isEmpty());
    }
    
}
Last modification:February 6th, 2020 at 09:55 pm
如果觉得我的文章对你有用,请随意赞赏