数据结构_双向链表的java实现

这篇文章讲述的是数据结构部分的双向链表的java实现,如有错误或者不当之处,还望各位大神批评指正。

双向链表的特点

  • 物理结构不连续逻辑结构连续
  • 删除和添加操作方便
  • 顺序储存随数据量的增大而增大
  • 查询操作不方便
  • 查询前驱后继元素比较方便

双向链表的基本操作

  1. init:初始化顺序表
  2. destroy:销毁数据表
  3. clear:清空数据表中的元素
  4. length:获取数据表长度
  5. get:获取索引位置的元素
  6. locateElem:定位元素
  7. priorElem:获取元素前驱
  8. nextElem:获取元素后继
  9. put:顺序放入元素
  10. insert:插入元素
  11. replace:替换元素
  12. delete:删除元素
  13. isEmpty:判断顺序表是否为空
  14. print:打印顺序表中的元素

双向链表的java实现

package list;

/**
 * ================================数据结构说明:========================================
 * 
 * 名称:双向链表
 * 
 * 特征:
 *      1. 物理结构不连续逻辑结构连续
 *      2. 删除和添加操作方便
 *      3. 顺序储存随数据量的增大而增大
 *      4. 查询操作不方便
 *      5. 查询前驱后继元素比较方便
 * 
 * 主要方法:
 *      1. init:初始化顺序表
 *      2. destroy:销毁数据表
 *      3. clear:清空数据表中的元素
 *      4. length:获取数据表长度
 *      5. get:获取索引位置的元素
 *      6. locateElem:定位元素
 *      7. priorElem:获取元素前驱
 *      8. nextElem:获取元素后继
 *      9. put:顺序放入元素
 *      10. insert:插入元素
 *      11. replace:替换元素
 *      12. delete:删除元素
 *      13. isEmpty:判断顺序表是否为空
 *      14. print:打印顺序表中的元素
 * 
 * ======================================================================================
*/
/**
 * @author 叶清逸
 * @date 2018年7月24日下午6:53:51
 * @version 1.0
 * @project list
 */
public class DouLinkList implements List {
    /*双向链表的长度*/
    private int LENGTH ;
    /*头指针*/
    private Element head ;

    /*元素类*/
    private class Element{
        Object data ;
        Element next ;
        Element prior ;
    }
    /**
     * @see list.List#init()
     * @explain init方法:初始化双向链表
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午6:59:22
     */
    @Override
    public void init() {
        head = new Element() ;
        head.data = null ;
        head.next = null ;
        head.prior = null ;

        LENGTH = 0 ;
    }
    /**
     * @see list.List#destroy()
     * @explain destroy方法:销毁双向链表
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午7:00:57
     */
    @Override
    public void destroy() {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return ;
        }
        head= null ;
    }
    /**
     * @see list.List#clear()
     * @explain clear方法:清空双向链表
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午7:40:53
     */
    @Override
    public void clear() {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return ;
        }
        head.next = null ;
        head.prior = null ;
    }
    /**
     * @see list.List#length()
     * @explain length方法:获取双向链表长度
     * @return 链表的长度
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午7:03:23
     */
    @Override
    public int length() {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return -1;
        }

        return LENGTH ;
    }
    /**
     * @see list.List#get(int)
     * @explain get方法:获取缩影位置的元素
     * @param index 索引
     * @return
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午7:41:15
     */
    @Override
    public Object get(int index) {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return null;
        }
        /*判断index是否合法*/
        if(index > LENGTH || index<=0){
            System.out.println("错误:索引不合法!");
            return null;
        }
        Element p = head.next ;
        /*找到索引位置*/
        for(int i=0 ; i<index-1 ; i++){
            p = p.next ;
        }
        return p.data;
    }
    /**
     * @see list.List#locateElem(java.lang.Object)
     * @explain locateElem方法:获取元素的索引
     * @param elem  元素
     * @return  元素的索引位置,若未找到则返回-1
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午7:49:42
     */
    @Override
    public int locateElem(Object elem) {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return -1;
        }

        int index = -1 ;
        /*遍历找到元素的位置*/
        Element p = head.next ;
        for(int i=0 ; i<LENGTH ; i++){
            if(p.data.equals(elem)){
                return i+1 ;
            }
            p = p.next ;
        }
        return index ;
    }
    /**
     * @see list.List#priorElem(java.lang.Object)
     * @explain priorElem方法:获取元素的前驱
     * @param elem 元素
     * @return 元素的前驱
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午7:57:03
     */
    @Override
    public Object priorElem(Object elem) {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return null;
        }

        Element prior = null ;
        /*定位到元素*/
        Element p = head.next ;
        while(p != null){
            if(p.data.equals(elem)){
                break ;
            }
            p = p.next ;
        }
        /*获取前驱*/
        if(p != null){
            prior = p.prior ;
            return prior.data;
        }else{
            return null ;
        }
    }

    @Override
    public Object nextElem(Object elem) {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return null;
        }
        Element next = null ;
        /*定位到元素*/
        Element p = head.next ;
        while(p != null){
            if(p.data.equals(elem)){
                break ;
            }
            p = p.next ;
        }
        if(p.next != null){
            next = p.next ;
            return next.data;
        }else{
            return null ;
        }
    }
    /**
     * @see list.List#put(java.lang.Object)
     * @explain put方法:添加元素
     * @param elem 要添加的元素
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午7:04:06
     */
    @Override
    public void put(Object elem) {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return ;
        }
        /*申请空间将元素存入*/
        Element e = new Element() ;
        e.data = elem ;
        /*遍历找出要插入的位置*/
        Element p = head ;
        while(p.next != null){
            p = p.next ;
        }
        /*改变指针指向*/
        e.next = p.next ;
        e.prior = p ;
        p.next = e ;

        LENGTH++ ;
    }
    /**
     * @see list.List#insert(int, java.lang.Object)
     * @explain insert方法:在索引位置插入元素
     * @param index 索引
     * @param elem 要插入的元素
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午8:14:43
     */
    @Override
    public void insert(int index, Object elem) {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return ;
        }
        /*判断index是否合法*/
        if(index > LENGTH || index<=0){
            System.out.println("错误:索引不合法!");
            return ;
        }
        /*找到索引位置*/
        Element p = head ;
        for(int i=0 ; i<index-1 ; i++){
            p = p.next ;
        }
        /*插入元素*/
        Element e = new Element() ;
        e.data = elem ;
        e.next = p.next ;
        e.prior = p ;
        p.next = e ;
        e.next.prior = e ;

        LENGTH++ ;
    }
    /**
     * @see list.List#replace(int, java.lang.Object)
     * @explain replace方法:将索引位置的元素替换为传入元素
     * @param index 索引位置
     * @param elem 替换的元素
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午9:43:46
     */
    @Override
    public void replace(int index, Object elem) {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return ;
        }
        /*判断index是否合法*/
        if(index > LENGTH || index<=0){
            System.out.println("错误:索引不合法!");
            return ;
        }
        /*找到索引位置*/
        Element p = head ;
        for(int i=0 ; i<index ; i++){
            p = p.next ;
        }
        /*替换元素*/
        p.data = elem ;
    }

    @Override
    public void delete(int index) {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return ;
        }
        /*判断index是否合法*/
        if(index > LENGTH || index<=0){
            System.out.println("错误:索引不合法!");
            return ;
        }
        /*找到索引位置*/
        Element p = head ;
        for(int i=0 ; i<index-1 ; i++){
            p = p.next ;
        }
        /*删除元素*/
        p.next = p.next.next ;
        p.next.prior = p ;

        LENGTH-- ;
    }
    /**
     * @see list.List#isEmpty()
     * @explain isEmpty方法:判断链表是否为空
     * @return 返回Boolean类型值,true为空,false为非空
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午9:52:53
     */
    @Override
    public boolean isEmpty() {
        boolean flag = false ;

        /*判断是否为空*/
        if(head.next == null)
            flag = true ;

        return flag;
    }
    /**
     * @see list.List#print()
     * @explain print方法:打印链表,顺序访问各个节点
     * @throws 
     * @author 叶清逸
     * @date 2018年7月24日 下午9:55:34
     */
    @Override
    public void print() {
        /*若未初始化输出异常信息*/
        if(head == null){
            System.out.println("错误:链表未初始化!");
            return ;
        }
        /*顺序访问各节点*/
        Element p = head.next ;
        while(p != null){
            System.out.print(p.data+" ");
            p = p.next ;
        }
        System.out.println();
    }
    @Override
    public String toString() {
        String str = "DouLinkList:[" ;
        Element p = head.next ;
        while(p != null){
            if(p.next != null){
                str = str + p.data +"," ;
                p = p.next ;
            }else{
                str = str + p.data ;
                p = p.next ;
            }

        }
        str = str+"]" ;
        return str ;
    }

}

猜你喜欢

转载自blog.csdn.net/u013634252/article/details/81230099