Java数据结构与算法分析《五》双端链表和双向链表

来源:互联网 发布:网络性质的公司 编辑:程序博客网 时间:2024/06/05 18:36
什么是双向链表每个节点节点保存了对于下一个节点的引用 同时还保存对前一个节点的引用 二是 从头部进行插入 要对链表进行判断 如果为空设置尾节点为新添加的节点 如果不为空还需要设置头结点的前一个节点为新添加的节点 三是从尾部进行插入 如果链表为空没设置头结点为新添加的节点 否则设置为节点的后一个节点为新天际的节点 同时设置新添加的节点的前一个节点为尾节点 从尾部进行删除 判断头结点是否有下一个节点 如果没有则设置节点为null 否则设置头结点的下一个节点的previous为null 从尾部进行删除如果头结点其他节点,则设置尾节点为null 否则设置为节点前一个节点的nextnull 设置为节点为其前一个节点删除方法不需要设置在使用一个临时的指针域
//链接点public class Node{    public long data;//数据域    public Node next;//指针域    public Node previous;    public Node(long value){        this.data =value;    }    //显示    public void display(){        System.out.println(data+"");    }}//双向链表public classDoubleLinkList{    private Node first;    private Node last;    public Node last(){        first =null;        last =null;    }    //插入一个节点    public void insertFirst(long value){        Node node = new Node(value);        if(isEmpty()){            last =node;        } else{            first.previous=node;        }    }    //插入一节节点 从尾节点进行插入    public void insertLast(long value){        Node node = new Node(value);        if(isEmpty()){            first = node;        } else{            last.next =node;            node.previous =last;        }    }    //删除一个节点 从头结点进行删除    public Node deleteFirst(){        Node temp = first;        if(first.next ==null){            last =null;        }else{            first.next.previous=null;        }        first =temp.next;        return temp;    }    //删除一个节点 在尾结点后面删除    public Node deleteLast(){        Node temp = last;        if(first.next == null){            last = null;        } else {            last.previous.next =null;        }        last =last.previous;         return last;    }    //显示方法    public void display(){        Node current = first;        while(current!=null){            current.display();            current =current.next;        }        System.out.println();    }    //查找方法    public Node  find(long value){        Node current = first;        while(current.data!=null){            if(current.next==null){                return null;            }                       current = current.next;             }        return current;    }    //删除方法 根据数据域来删除    public Nide delete(long value){        Node current =first;        while(current.data!=value){            if(current.next==null){                return null;            }            current =current.next;        }        if(current ==first){            first =first.next;        } else{            current.previous.next =current.next;        }        return current;    }    //判断是否为空    public boolean isEmpty(){        return (first==null);    }}
1.什么是双端链表链表中保存着对最后一个链接点引用的链表2.从头部进行插入要对链表进行判断 如果为空则设置尾节点为新添加的节点3.从尾部进行插入如果链表为空 则设置头结点为新添加的节点否则设置为节点的后一个节点为新添加的节点4.从头部进行删除判断头结点是否有下一个节点如果没有则设置节点为null
//链接点public class Node{    public long data;//数据域    public Node next;//指针域    public Node previous;    public Node(long value){        this.data =value;    }    //显示    public void display(){        System.out.println(data+"");    }   }//双端链表public class FirstLastList{    private Node first;    private Node last;    public FirstLastList(){        first =null;        last =null;    }    //插入一个节点 从头结点进行插入    public void insertFirst(long value){        Node node = new Node(value);        if(isEmpty(()){            last =node;        }        node.next =first;        first =node;    }    //插入一个节点 从尾部节点进行插入    public void insertLast(){        Node node = new Node(value);        if(isEmpty()){            first =node;        } else{            last.next=node;        }        last =node;    }    //删除一个节点 在头结点后进行删除    public Node deleteFirst(){        Node temp =first;        if(first.next==null){            last =null;        }        first = temp.next;        return temp;    }    //显示方法    public void display(){        Node current =first;        while(current!=null){            current.display();            current= current.next;        }        System.out.println();    }    //查找方法    public Node find(long value){        Node current =first;        while(current.data!=value){            if(current.next==null){                return null;            }            current =current.next;        }        return current;    }    //删除方法    public Node delete(long value){        Node current =first;        Node previous =first;        while(current.data!=value){            if(current.next==null){                return null;            }            previous =current;            current =current.next;        }        if(current==first){            first =first.next;        }else{            previous.next =current.next;        }        return current;    }}
0 0
原创粉丝点击