Skip to content

Latest commit

 

History

History
193 lines (161 loc) · 6.35 KB

04.单链表创建和使用.md

File metadata and controls

193 lines (161 loc) · 6.35 KB

目录介绍

  • 01.单链表操作
  • 02.单链表实现代码
  • 03.单链表测试

好消息

  • 博客笔记大汇总【15年10月到至今】,包括Java基础及深入知识点,Android技术博客,Python学习笔记等等,还包括平时开发中遇到的bug汇总,当然也在工作之余收集了大量的面试题,长期更新维护并且修正,持续完善……开源的文件是markdown格式的!同时也开源了生活博客,从12年起,积累共计N篇[近100万字,陆续搬到网上],转载请注明出处,谢谢!所有博客陆续更新到GitHub上!
  • 链接地址:https://github.com/yangchong211/YCBlogs
  • 如果觉得好,可以star一下,谢谢!当然也欢迎提出建议,万事起于忽微,量变引起质变!

01.单链表操作

  • 插入
    • 插入一个节点,对于单向链表,我们只提供在链表头插入,只需要将当前插入的节点设置为头节点,next指向原头节点即可。
  • 删除
    • 删除一个节点,我们将该节点的上一个节点的next指向该节点的下一个节点。
  • 查找
    • 单向链表只可向一个方向遍历,一般查找一个节点的时候需要从第一个节点开始每次访问下一个节点,一直访问到需要的位置。

02.单链表实现代码

  • 实现代码
    • 关于单链表添加,删除,查找等操作的代码
    public class SingleLinkedList {
        private int size;//链表节点的个数
        private Node head;//头节点
        
        public SingleLinkedList(){
            size = 0;
            head = null;
        }
        
        //链表的每个节点类
        private class Node{
            private Object data;//每个节点的数据
            private Node next;//每个节点指向下一个节点的连接
            
            public Node(Object data){
                this.data = data;
            }
        }
        
        //在链表头添加元素
        public Object addHead(Object obj){
            Node newHead = new Node(obj);
            if(size == 0){
                head = newHead;
            }else{
                newHead.next = head;
                head = newHead;
            }
            size++;
            return obj;
        }
        
        //在链表头删除元素
        public Object deleteHead(){
            Object obj = head.data;
            head = head.next;
            size--;
            return obj;
        }
        
        //查找指定元素,找到了返回节点Node,找不到返回null
        public Node find(Object obj){
            Node current = head;
            int tempSize = size;
            while(tempSize > 0){
                if(obj.equals(current.data)){
                    return current;
                }else{
                    current = current.next;
                }
                tempSize--;
            }
            return null;
        }
        
        //删除指定的元素,删除成功返回true
        public boolean delete(Object value){
            if(size == 0){
                return false;
            }
            Node current = head;
            Node previous = head;
            while(current.data != value){
                if(current.next == null){
                    return false;
                }else{
                    previous = current;
                    current = current.next;
                }
            }
            //如果删除的节点是第一个节点
            if(current == head){
                head = current.next;
                size--;
            }else{//删除的节点不是第一个节点
                previous.next = current.next;
                size--;
            }
            return true;
        }
        
        //判断链表是否为空
        public boolean isEmpty(){
            return (size == 0);
        }
        
        //显示节点信息
        public void display(){
            if(size >0){
                Node node = head;
                int tempSize = size;
                if(tempSize == 1){//当前链表只有一个节点
                    System.out.println("["+node.data+"]");
                    return;
                }
                while(tempSize>0){
                    if(node.equals(head)){
                        System.out.print("["+node.data+"->");
                    }else if(node.next == null){
                        System.out.print(node.data+"]");
                    }else{
                        System.out.print(node.data+"->");
                    }
                    node = node.next;
                    tempSize--;
                }
                System.out.println();
            }else{//如果链表一个节点都没有,直接打印[]
                System.out.println("[]");
            }
        }
    }
    

03.单链表测试

  • 测试代码
    public void testSingleLinkedList(){
        SingleLinkedList singleList = new SingleLinkedList();
        singleList.addHead("A");
        singleList.addHead("B");
        singleList.addHead("C");
        singleList.addHead("D");
        //打印当前链表信息
        singleList.display();
        //删除C
        singleList.delete("C");
        singleList.display();
        //查找B
        System.out.println(singleList.find("B"));
    }
    
  • 打印结果

其他介绍

01.关于博客汇总链接

02.关于我的博客