链表的实现

1.概念:

链表是一种物理结构上非连续的存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。

2.一些基本方法:

1.void.addFirst();头插法

2.void.addLast();尾插法

3.void.addIndex(int index, int data);任意位置插入,第一个数据节点为0号下标;

4.boolean.contains(int key);判断是否包含元素 key

5.void.remove(int key);删除第一次出现为key的节点

6.void.removeAll(int key);删除所有值为key的节点

3. 实现代码(以int类型数据为例):

class ListNode{
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}

public class MyLinkedList {
    public ListNode head;


    public MyLinkedList() {

    }

    //头插
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
        }else {
            node.next = this.head;
            this.head = node;
        }
    }

    //尾插
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
        }else{
            ListNode cur = this.head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //找前驱
    public ListNode searchPrev(int index){
        ListNode cur = this.head;
        int count = 0;
        while(count != index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    //指定位置插入
    public void addIndex(int index,int data){
        if(index < 0 || index > size()){
            System.out.println("插入位置无效!!");
            return;
        }
        ListNode node = new ListNode(data);
        if(this.head == null){
            return;
        }
        if(index == 0){
            addFirst(data);
        }else if(index == size()-1){
            addLast(data);
        }else {
            ListNode prev = searchPrev(index);
            node.next = prev.next;
            prev .next = node;
        }
    }

    //判断是否包含元素key
    public boolean contains(int key){
        ListNode cur = this.head;
        if(this.head == null){
            return false;
        }
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //找前驱
    public ListNode searchPrev2(int key){
        ListNode cur = this.head;
        while(cur.next != null){
            if(cur.next.val == key){
               return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除第一次出现的key元素
    public void remove(int key){
        ListNode cur = this.head;
        if(this.head.val == key){
            this.head = this.head.next;
        }else {
            ListNode prev = searchPrev2(key);
            while (cur != null) {
                if (cur.val == key) {
                    prev.next = cur.next;
                    return;
                }
                cur = cur.next;
            }
        }
    }

    //删除所有的key元素
    public void removeAllKey(int key){
        if(this.head == null){
            return;
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                ListNode prev = searchPrev2(key);
                prev.next = cur.next;
            }
            cur = cur.next;
        }
    }

    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while(cur != null){
             count++;
            cur = cur.next;
        }
        return count;
    }

    //打印单链表
    public void display(){
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //清空链表
    public void clear(){
        this.head = null;
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFirst(33);
        myLinkedList.addFirst(33);
        myLinkedList.addFirst(22);
        myLinkedList.addFirst(44);
        myLinkedList.addFirst(33);
        myLinkedList.display();
        
        myLinkedList.addLast(88);
        myLinkedList.display();
        
        System.out.println(myLinkedList.contains(111));
        
        myLinkedList.remove(33);
        myLinkedList.display();
        myLinkedList.removeAllKey(33);
        myLinkedList.display();
        
        myLinkedList.addIndex(7,99);
        myLinkedList.display();
        System.out.println(myLinkedList.size());
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值