Java数据结构与算法分析《四》链表

来源:互联网 发布:网络性质的公司 编辑:程序博客网 时间:2024/05/21 18:39

链接点

//链接点 相当于车厢public class Node{    //数据域    public long data;    //指针域    public Node next;    public Node(long value){        this.data = data;    }    //显示方法    public void display(){        System.out.println(data+" ");    }}

链表

//链表相当于火车public class LinkList{    //头结点    private Node first;    public LinkList(){        first = null;    }    //插入一个节点 在头结点后进行插入    public void insertFirst(long value){        Node node = new Node();        Node.next = first;        first = node;    }    //删除一个节点 在头结点后进行删除    public void deleteFirst(){        Node temp = first;        first = temp.next;        return temp;    }    //显示方法    public void display(){        Node current =frist;        while(current!=null){            current.display();            current =current.next();        }        System.out.println();           }    //查找方法    public Node find(long value){        Node current = first;        while(current.data!=null){            if(cuurent.next == null){                return null;            }            current = current.next;        }        return cuurent;    }    //删除方法 根据数据域来进行删除    public Node delete(long value){        Node current =first;        Node previous =first;        while(current.data!=value){            if(current.next == null){                return null;            }            previous =current;            cuurent = current.next;        }        if(current == first){            first = first.next;        } else {            previous.next = current.next;        }        return current;    }}
0 0