单链表(打印头插法\头插法\尾插法\查找是否包含关键字key是否在单链表当中\得到单链表的长度\测试contines和size\任意位置插入,第一个数据结点为0号下标......)

这篇博客介绍了如何使用Java实现单链表的各种操作,包括头插法、尾插法的打印,查找关键字,获取链表长度,以及在链表中插入元素。同时,讨论了删除特定关键字节点、释放内存、反转链表、找到链表中间节点、求链表倒数第k个节点、按值分割链表、删除重复节点、判断链表回文和环等问题的解决方案。
摘要由CSDN通过智能技术生成

单链表

在这里插入图片描述
在这里插入图片描述

head就是个引用,它保存的是234

 //头插法
        public void addFirst(int data) {
   
        Node node = new Node(data);//有一个node节点了

class Node {
   
    public int data;//0
    public Node next;//null
    public Node(int data) {
   
        this.data =data;//传入的数据  这样一来节点就有值了
        this.next = null;//可写可不写,引用类型默认为null
    }

}

在这里插入图片描述

class Node {
   
    public int data;//0
    public Node next;//null
    public Node(int data) {
   
        this.data =data;
        this.next = null;
    }

}

public class MyLinkedList {
   

    public Node head;//保存单链表的头节点的引用  null   用head标识头


        //头插法
        public void addFirst(int data) {
   
        Node node = new Node(data);//有一个node节点了

        if (this.head == null) {
   
            //第一次插入节点
            this.head = node;
            return;
        }
        node.next = this.head;//node的next指向head
        head = node;//head 指向头节点
        }

在这里插入图片描述

//head表示头节点的地址
public void display() {
    
            Node cur = this.head;//定义一个变量cur,它保存head的值(cur指向head的节点)
            while (cur != null) {
   
                System.out.print(cur.data + " ");
                cur = cur.next;//新的cur表示cur.next 把cur.next赋给cur  新的cur就指向下一个节点
            }

打印头插法

class Node {
   
    public int data;//0
    public Node next;//null
    public Node(int data) {
   
        this.data =data;
        this.next = null;
    }

}
public class MyLinkedList {
   
    public Node head;//保存单链表的头节点的引用  null
        //头插法
        public void addFirst(int data) {
   
        Node node = new Node(data);

        if (this.head == null) {
   
            //第一次插入节点
            this.head = node;
            return;
        }
        node.next = this.head;
        head = node;
        }
    //打印单链表
    public void display() {
   
        Node cur = this.head;
        while (cur != null) {
   
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
    }



public class TestDemo {
   
    public static void main(String[] args) {
   
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFirst(10);
        myLinkedList.addFirst(11);
        myLinkedList.addFirst(12);
        myLinkedList.addFirst(13);
        myLinkedList.display();
    }
}

在这里插入图片描述

打印头插法代码截图
在这里插入图片描述

在这里插入图片描述
尾插法

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

//尾插法
        public void addLast(int data) {
   
            Node node = new Node(data);//首先有一个节点
           
            if (this.head == null){
   //第一次插入时
                this.head = node;
                return;
            }
            Node cur = this.head;//定义一个cur从头开始走
            while (cur.next != null) {
   //cur.next = null时,cur指向最后一个节点
                cur = cur.next;//
            }//退出循环cur.next为空了,即就是尾节点
            cur.next = node;
        }




public class TestDemo {
   
    public static void main(String[] args) {
   
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(77);
        myLinkedList.addLast(78);
        myLinkedList.addLast(79);
        myLinkedList.addLast(80);



       /* myLinkedList.addFirst(10);
        myLinkedList.addFirst(11);
        myLinkedList.addFirst(12);
        myLinkedList.addFirst(13);*/
        myLinkedList.display();
    }
}

代码截图
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
查找是否包含关键字key是否在单链表当中
在这里插入图片描述

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

            return false;
        }

得到单链表的长度

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

测试contines和size

public class TestDemo {
   
    public static void main(String[] args) {
   
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(77);
        myLinkedList.addLast(78);
        myLinkedList.addLast(79);
        myLinkedList.addLast(80);

 

       /* myLinkedList.addFirst(10);
        myLinkedList.addFirst(11);
        myLinkedList.addFirst(12);
        myLinkedList.addFirst(13);*/
        myLinkedList.display();
        boolean flg = myLinkedList.contains(78);
        System.out.println(flg);
        System.out.println(myLinkedList.size());
    }
}

任意位置插入,第一个数据结点为0号下标
在这里插入图片描述

//任意位置插入,第一个数据结点为0号下标
        public void addIndex(int index, int data) {
   
            if(index == 0) {
   //头插法
                addFirst(data);
                return;
            }
            if (index == this.size()) {
   //尾插法
                addLast(data);
                return;

            }
            Node node = new Node(data);
            //先找到 index位置的前一个结点的地址
            Node cur = searchIndex(index);//调用它之后 就拿到了一个index-1步这样的地址
            //进行插入
            node.next =cur.next;
            cur.next = node;

        }

        private Node searchIndex(int index) {
   
        	//1.对index进行合法性检查
            if (index < 0 || index > this.size()) {
   
                throw new RuntimeException("index位置不合法");
            }
            Node cur = this.head;//让cur从头开始走 写个循环让cur 走index-1步  返回cur
            while (index - 1 != 0) {
   
                cur = cur.next;
                index--;
            }
            return cur;
        }




public class TestDemo {
   
    public static void main(String[] args) {
   
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(77);
        myLinkedList.addLast(78);
        myLinkedList.addLast(79);
        myLinkedList.addLast(80);

       /* myLinkedList.addFirst(10
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哈希表(Hash Table)是一种根据关键码值(Key-Value)而直接进行访问的数据结构,是一种以常数平均时间复杂度进行数据查找数据结构。哈希表的核心就是哈希函数,它能够将关键码值映射到哈希表中的一个位置,从而实现快速查找。 哈希表的冲突处理方法有多种,其中一种常见的方法是链式哈希表(Chained Hash Table),它使用链表来处理冲突。链式哈希表中的每个哈希桶(Hash Bucket)都是一个链表结点,相同哈希值的元素会被插入到对应哈希桶的链表中。 对于链式哈希表的插入操作,有两种常见的插入方式:头插法尾插法头插法:新插入的元素会被插入到对应哈希桶的链表部,成为新的结点。该方法的优点是插入速度快,因为只需要修改链表指针即可。缺点是查找元素时需要遍历整个链表,因为相同哈希值的元素往往会被插入链表部,导致链表长度变长。 尾插法:新插入的元素会被插入到对应哈希桶的链表的尾部,成为新的尾结点。该方法的优点是查找元素时只需要遍历链表的一部分,因为相同哈希值的元素往往会被插入链表的尾部,导致链表长度变短。缺点是插入速度慢,因为需要遍历整个链表才能找到链表的尾部。 综上所述,头插法适合于查找操作频繁,而尾插法适合于插入操作频繁的情况。具体使用哪种方法,需要根据实际情况进行选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值