自定义实现Java中的LinkedList类

手动实现LinkedList

代码实现

package Java_1230;

class Node{
    public int val;
    public Node next;
    public Node(){

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

}
public class MyLinkedList {
    public Node head; //表时当前链表的头

    public void createLinked(){
        this.head = new Node(12);
        Node node2 = new Node(22);
        Node node3 = new Node(32);
        Node node4 = new Node(42);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;

    }
    public void display(){
        Node cur = this.head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //找到尾节点
    public Node findLastNode(){
        Node cur = this.head;
        while(cur.next!=null){
            cur = cur.next;
        }
        return cur;
    }
    public int Size(){
        Node cur = this.head;
        int count=0;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public boolean contains(int key){
        Node cur = this.head;
        while(cur!=null){
            if(cur.val ==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
        //0.首先你得有一个节点
        Node node = new Node(data);
        //1.判断链表是不是空
        if(this.head==null){
            this.head = node;
        }else {

            /**
             * 在任何情况下,插入的时候,先绑定后面
             */
            //2.插入
            node.next = this.head;
            this.head = node;
//            优化版
//            Node node = new Node(data);
//            node.next = this.head;
//            this.head = node;
        }

    }
    public void addLast(int data){
        Node  node= new Node(data);
        //判空
        if(this.head==null){
            this.head=node;
        }else {
//            Node cur = this.head;
//            while(cur.next!=null){
//                cur = cur.next;
//            }
//            cur.next = node;
            findLastNode().next = node;
        }
    }
    public void addIndex(int index,int data){
        if(index<0||index>Size()){
            System.out.println("index不合法");
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==Size()-1){
            addLast(data);
            return;
        }
        Node cur= moveIndex(index);
        //cur保存的是index-1位置的节点
        Node node = new Node(data);
        node.next= cur.next;
        cur.next = node;
    }
    /**
     * 该函数是找到index-1位置的节点的引用
     * @param index
     * @return
     */
    public Node moveIndex(int index){
        Node cur = this.head;
        int count =0;
        while(count != index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    /**
     * 找到关键字key的前驱,如果有返回前驱节点的引用
     * 如果没有返回null
     * @param key
     * @return
     */
    public Node searchPrev(int key){
        Node cur = this.head;
        while(cur.next!=null){
            if(cur.next.val ==key){
                return cur;
            }
            cur = cur.next;
        }
        return null;//代表没有要删除的节点的前驱
    }
    public void remove(int key){
        //判空
        if(this.head==null){
            return;
        }
        //删除头结点
        if(this.head.val==key){
            this.head = this.head.next;
            return;
        }
        //正常删除
        Node cur = searchPrev(key);
        if(cur ==null){
            System.out.println("删除失败,没有Key的前驱");
        }else {
            Node del = cur.next;
            cur.next = del.next;
        }
    }
    public void removeAllKey(int key){
        if(this.head==null){
            return;
        }
        //正常删除
        Node prev = this.head;
        Node cur = prev.next;
        while (cur!=null){
            if(cur.val==key){
                prev.next = cur.next;
            }else {
                prev =cur;
            }
            cur = cur.next;
        }
        //如果头结点 = key,删除头结点
        if(this.head.val==key){
            this.head = this.head.next;
        }
    }
    public void clear(){
        this.head = null;
    }
    //反转链表
    public Node reverseList(){
        Node cur = this.head;
        Node prev = null;
        Node newHead = null;
        while(cur!=null){
            Node curNext = cur.next;
            if(curNext==null){
                newHead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return newHead;
    }
    public Node middleNode(){
        int len = Size()/2;
        Node cur =this.head;
        int count = 0;
        while(count!=len){
            cur =cur.next;
            count++;
        }
        return cur;
    }
    public Node middleNodeplus(){
        Node slow = this.head;
        Node fast = this.head;
        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

}

测试代码

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

        MyLinkedList myLinkedList = new MyLinkedList();
        //这条代码结束,获得链表的头结点
        myLinkedList.createLinked();
        myLinkedList.display();
        System.out.println("=================");
        Node cur = myLinkedList.findLastNode();
        System.out.println(cur.val);
        System.out.println("=================");
        System.out.println(myLinkedList.Size());
        System.out.println("=================");
        System.out.println(myLinkedList.contains(42));
        myLinkedList.addLast(3);
        myLinkedList.display();
        System.out.println("================");
        myLinkedList.addIndex(2,21);
        myLinkedList.display();
        System.out.println("================");
        myLinkedList.remove(22);
        myLinkedList.display();
        Node ret =myLinkedList.middleNodeplus();
        System.out.println(ret.val);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值