单链表的实现和测试(JAVA)

单链表的实现

class Node {
    public int data;
    public Node next;

    public Node(int data){
        this.data = data;
    }
}
class MyLinkList {
    public Node head;
    public MyLinkList(){
        this.head = null;
    }
    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        if(head == null){
            head = node;
            head.next = null;
            return ;
        }else {
            node.next = head;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        Node cur = this.head;

        //判断头结点是否为空
        if(head == null){
            head = node;
            head.next = null;
            return ;
        }
        //找到尾结点
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
        node.next = null;
    }
    //任意位置插入,第一个数据节点为0号下标

    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new IndexOutOfBoundsException("Index越界");
        }
    }

    private Node foundIndex(int index){
        Node cur = this.head;
        int count = 0;
        while(cur != null && count < index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }
    public boolean addIndex(int index,int data){
        //检查index合法性,checkIndex(index);
        //找到index-1节点,foundIndex-1()
        checkIndex(index);
        Node cur = foundIndex(index);
        Node node = new Node(data);
        if(index == 0){
            addFirst(data);
            return true;
        }
        else if(index == size()){
            addLast(data);
            return true;
        } else{
            node.next = cur.next;
            cur.next = node;
            return true;
        }
    }

    //查找是否包含关键字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 && cur.next != null){
            //判断头节点
            if(cur.data == key && cur == head){
                head = cur.next;
                return;
            }
            //不是头结点
            else if(cur.next.data == key){
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node cur = this.head;
        while(cur != null && cur.next != null){
            //判断头节点
            if(cur.data == key && cur == head){
                head = cur.next;
            }
            //不是头结点
            else if(cur.next.data == key){
                cur.next = cur.next.next;
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        Node cur = this.head;
        int count = 0;
        if(head == null){
            return 0;
        }
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        Node cur = this.head;
        while(cur != null){
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        this.head = null;
    }
}

测试

public class Text {
    public static void main(String[] args) {

        MyLinkList myLinkList = new MyLinkList();
        myLinkList.addFirst(1);
        myLinkList.addFirst(2);
        myLinkList.addFirst(3);
        myLinkList.addFirst(3);
        myLinkList.addLast(5);
        myLinkList.display();
        System.out.println("长度:"+myLinkList.size());
        myLinkList.addIndex(3,4);
        myLinkList.display();
        myLinkList.remove(4);
        myLinkList.display();
        myLinkList.removeAllKey(3);
        myLinkList.display();
        System.out.println("清空单链表====");
        myLinkList.clear();
        myLinkList.display();
        System.out.println("清空单链表====");
    }
}

测试结果

3 3 2 1 5
长度:5
3 3 2 4 1 5
3 3 2 1 5
2 1 5
清空单链表====

清空单链表====
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
程序 = 数据结构 + 算法  程序是为了解决实际问题而存在的。然而为了解决问题,必定会使用到某些数据结构以及设计一个解决这种数据结构的算法。如果说各种编程语言是程序员的招式,那么数据结构和算法就相当于程序员的内功。编程实战算法,不是念PPT,我们讲的就是实战与代码实现与企业应用。程序 = 数据结构 + 算法                ——图灵奖得主,计算机科学家N.Wirth(沃斯)作为程序员,我们做机器学习也好,做python开发也好,java开发也好。有一种对所有程序员无一例外的刚需 —— 算法与数据结构日常增删改查 + 粘贴复制 + 搜索引擎可以实现很多东西。同样,这样也是没有任何竞争力的。我们只可以粘贴复制相似度极高的功能,稍复杂的逻辑没有任何办法。语言有很多,开发框架更是日新月异3个月不学就落后我们可以学习很多语言,很多框架,但招聘不会考你用5种语言10种框架实现同一个功能。真正让程序员有区分度,企业招聘万年不变的重点 —— 算法与数据结构。算法代表程序员水平的珠穆朗玛。如果说各种编程语言是程序员的招式,那么数据结构和算法就相当于程序员的内功。 想写出精炼、优秀的代码,不通过不断的锤炼,是很难做到的。 开这个系列的目的是为了自我不断积累。不积跬步无以至千里嘛。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值