java_note_10

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: zhuzhuzhuchao
 * Date: 2022-01-03
 * Time: 12:19
 */
public class TestDemo {
    // 链表
    // 一个一个节点组成
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.createList();
        myLinkedList.display();

        System.out.println(myLinkedList.contains(12));
        System.out.println(myLinkedList.contains(21));

        System.out.println(myLinkedList.size());

        myLinkedList.addFirst(1);
        myLinkedList.display();

        myLinkedList.addLast(67);
        myLinkedList.display();

        myLinkedList.addIndex(0,99);
        myLinkedList.display();
        myLinkedList.addIndex(3,99);
        myLinkedList.display();

        myLinkedList.remove(56);
        myLinkedList.display();
    }
}
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: zhuzhuzhuchao
 * Date: 2022-01-03
 * Time: 13:13
 */
public class MyLinkedList {

    public ListNode head;//链表的头引用



    public void createList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        this.head = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;

    }



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



    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);

    }



    //头插法
    public void addFirst(int data){
        ListNode listNode = new ListNode(data);
        listNode.next = this.head;
        this.head = listNode;
    }



    //尾插法
    // 第一次插入必须判断是不是空
//    public void addLast(int data){
//        ListNode cur = this.head;
//        ListNode listNode = new ListNode(data);
//        if (cur == null) {
//            cur.val = data;
//        } else {
//            while (cur.next != null) {
//                cur = cur.next;
//            }
//            cur.next = listNode;
//        }
//    }
    // 博哥写
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if (this.head == null) {
            this.head = node;
        } else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }




    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index < 0 || index >size()) {
            System.out.println("位置不合法");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }

        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while (index-1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }



    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }



    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            System.out.println("空");
            return;
        }
       if (this.head.val == key) {
           this.head = this.head.next;
           return;
       }
       ListNode prev = searchPerv(key);
       if (prev == null) {
           System.out.println("没有");
           return;
       }
       ListNode del = prev.next;
       prev.next = del.next;
    }
    public ListNode searchPerv(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }



    //删除所有值为key的节点
    public ListNode removeAllKey(int key) {
        if (this.head == null) {
            return null;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        // 最后处理头

        if (this.head.val == key) {
            this.head = this.head.next;
        }
        return this.head;
    }



    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }



    public void clear() {
        // 粗暴
        // this.head = null;

        while (this.head != null) {
            ListNode curNext = this.head.next;
            this.head.next = null;
            this.head = curNext;
        }
        this.head = null;
    }
}





// ListNode代表一个节点
// 单向不带头非循环
// 不带头:头一直在变
// 循环:最后一个的next是第一个的地址
class ListNode {
    public  int val;
    public ListNode next;//存节点地址

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值