双向链表的实现和测试(JAVA)

双向链表的实现

class Node{
    public Node next;
    public Node prey;
    public int data;

    public Node(int data){
        this.data = data;
    }
}

class DoubleList{
    public Node head;
    public Node last;
    public DoubleList(){
        this.head = null;
        this.last = null;
    }
    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        if(head == null){
            this.head = node;
            this.last = node;
        }else{
            head.prey = node;
            node.next = head;
            head =  node;
            head.prey = null;
        }
    }

    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if(head == null){
            this.head = node;
            this.last = node;
        }else{
            last.next = node;
            node.prey = last;
            last = node;
        }
    }

    //任意位置插入,第一个数据节点为0号下标

    //检查index合法性
    private void checkIndex(int index){
        if(index < 0 || index > size()) {
            throw new IndexOutOfBoundsException("index越界异常");
        }
    }
    //找对应下标节点
    private Node foundIndex(int index){

        int count = 0;
        Node cur = this.head;
        while(count < index){
            count++;
            cur = cur.next;
        }
        return cur;
    }
    public boolean addIndex(int index,int data){
        //首先要找到对应下标节点并检查合法性,checkIndex();foundIndex();
        checkIndex(index);
        Node node = new Node(data);
        Node cur = foundIndex(index);
        if(cur == head){
            addFirst(data);
            return  true;
        }else if(cur == last){
            addLast(data);
        }
        node.next = cur;
        node.prey = cur.prey;
        cur.prey.next = node;
        cur.prey = node;
        return true;
    }

    //求双向链表长度
    public int size(){
        Node cur = this.head;
        int count = 0;
        if(head == null){
            return 0;
        }
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }
    //查找是否包含关键字key是否在双向链表当中
    public boolean contains(int key){
        Node cur = this.head;
        while(cur != null){
            if(cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        Node cur = this.head;
        while(cur != null){
            //头结点
            if(cur.data == key && cur == head){
                head = head.next;
                head.prey = null;
                return ;
            }
            //尾节点
            else if(cur.data == key && cur == last){
                last = last.prey;
                last.next = null;
                return ;
            }
            //中间节点
            else if(cur.data == key){
                cur.prey.next = cur.next;
                cur.next.prey = cur.prey;
                return ;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node cur = this.head;
        while(cur != null){
            //头结点
            if(cur.data == key && cur == head){
                head = head.next;
                head.prey = null;
            }
            //尾节点
            else if(cur.data == key && cur == last){
                last = last.prey;
                last.next = null;
            }
            //中间节点
            else if(cur.data == key){
                cur.prey.next = cur.next;
                cur.next.prey = cur.prey;
            }
            cur = cur.next;
        }
    }
    //展示双向链表
    public void display(){
        Node cur = this.head;
        while(cur != null){
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //清空双向链表(不允出现内存泄漏)
    public void clear(){
        Node cur = this.head;

        while(cur != null){
            Node curNext = cur.next;
            cur.prey = null;
            cur.next = null;
            cur = curNext;
        }
        //防止内存泄漏
        this.head = null;
        this.last = null;
    }
}

测试

public class TestDamo{
    public static void main(String[] args) {
        DoubleList doubleList = new DoubleList();
        doubleList.addFirst(1); //头插
        doubleList.addFirst(2); //头插
        doubleList.addFirst(3); //头插
        doubleList.addFirst(4); //头插
        doubleList.addFirst(5); //头插
        doubleList.addFirst(6); //头插
        doubleList.addLast(3);  //尾插
        doubleList.display();
        System.out.println("长度:"+doubleList.size());
        doubleList.addIndex(2,10); //中间插
        doubleList.display();
        doubleList.remove(2);//删除第一次出现data为2的节点
        doubleList.display();
        doubleList.removeAllKey(3);//删除所有data为3的节点
        doubleList.display();
        System.out.println("========下面是清空双向链表");
        doubleList.clear();
        doubleList.display();
    }
}

测试结果

6 5 4 3 2 1 3
长度:7
6 5 10 4 3 2 1 3
6 5 10 4 3 1 3
6 5 10 4 1
========下面是清空双向链表

========上面是清空双向链表
1.算法是程序的灵魂,优秀的程序在对海量数据处理时,依然保持高速计算,就需要高效的数据结构算法支撑。2.网上数据结构算法的课程不少,但存在两个问题:1)授课方式单一,大多是照着代码念一遍,数据结构算法本身就比较难理解,对基础好的学员来说,还好一点,对基础不好的学生来说,基本上就是听天书了2)说是讲数据结构算法,但大多是挂羊头卖狗肉,算法讲的很少。 本课程针对上述问题,有针对性的进行了升级 3)授课方式采用图解+算法游戏的方式,让课程生动有趣好理解 4)系统全面的讲解了数据结构算法, 除常用数据结构算法外,还包括程序员常用10大算法:二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法、马踏棋盘算法。可以解决面试遇到的最短路径、最小生成树、最小连通图、动态规划等问题及衍生出的面试题,让你秒杀其他面试小伙伴3.如果你不想永远都是代码工人,就需要花时间来研究下数据结构算法。教程内容:本教程是使用Java来讲解数据结构算法,考虑到数据结构算法较难,授课采用图解加算法游戏的方式。内容包括: 稀疏数组、单向队列、环形队列、单向链表双向链表、环形链表、约瑟夫问题、栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、八皇后问题、算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查找、插值查找、斐波那契查找、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查找树(B树B+树B*树)、图、图的DFS算法BFS、程序员常用10大算法、二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法。学习目标:通过学习,学员能掌握主流数据结构算法的实现机制,开阔编程思路,提高优化程序的能力。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值