2021-11-08-双向链表

作业

  1. class Test{
        public static void hello(){
            System.out.println("hello");
        }
    }
    public class MyApplication {
        public static void main(String[] args) {
            Test test=null;
            test.hello();
        }
    }
    

    静态的方法,不依赖于对象的

  2. public class MyApplication {
        String s;
        public static void main(String[] args) {
            String s;
            MyApplication myApplication = new MyApplication();
    //        System.out.println(s);报错
            System.out.println(myApplication.s);
        }
    }
    

    局部变量需要初始化,否则就报错,成员变量不初始化的话,会自动赋值默认值,例如上面的成员变量s,默认赋值为null;

  3. public class Test { 
        public int aMethod(){
            static int i = 0;
            i++; 
            return i;
        } 
    public static void main(String args[]){
        Test test = new Test(); 
        test.aMethod(); 
        int j = test.aMethod();
        System.out.println(j);
        } 
    }
    

    方法中的变量i是静态变量,但是方法不是静态,编译失败。

  4. public class Pvf{
        static boolean Paddy;
        public static void main(String args[]){
            System.out.println(Paddy);
        }
    编译成功
    

双向链表

  1. 双向链表的节点构造

    class ListNode{
        private int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }
    
  2. 创造头指针和尾指针

        public ListNode head;
        public ListNode last;
    
  3. 头插法

        public void addFirst(int data){
            ListNode node = new ListNode(data);
            if (head==null){
                head=node;
                last=node;
            }else{
                head.prev=node;
                node.next=head;
                head=node;
            }
        }
    
  4. 尾插法

        public void addLast(int data){
            ListNode node = new ListNode(data);
            if (head==null){
                head=node;
                last=node;
            }else{
                last.next=node;
                node.prev=last;
                last=node;
            }
        }
    
  5. 删除第一次出现Key的节点

        public void remove(int key){
            ListNode cur=head;
            while(cur!=null){
                if (cur.val==key){
                    if (cur==head){
                        head=head.next;
                        head.prev=null;
                        return;
                    }else if (cur==last){
                        cur.prev.next=cur.next;
                        last=last.prev;
                        return;
                    }else{
                        cur.prev.next=cur.next;
                        cur.next.prev=cur.prev;
                        return;
                    }
                }else {
                    cur=cur.next;
                }
            }
            System.out.println("该链表为空");
        }
    

    head=head.next;head.prev=null;head前面的节点没有被引用就会被jvm回收

    上面的代码有一处代码会报空指针

    head=head.next;head.prev=null;
    

    如果此时head==null,那么head.prev就是空指针异常了,改进办法就是:

    					head=head.next;
                        if (head!=null){
                        head.prev=null;
                        }
                        else{
                            last=null;
                        }
    
  6. 删除所有出现Key的节点

    将上面的代码中的return去掉

  7. 任意位置插入一个数据

        public int size(){
            return 0;
        }
        public ListNode searchIndex(int index){
            ListNode cur =head;
            if (index<0||index>size()){
                System.out.println("你输入数据不对");
                return null;
            }
            while(index!=0){
                cur=cur.next;
                index--;
            }
            return cur;
        }
        public void addIndex(int index,int data){
            ListNode node =new ListNode(data);
            if (index==0){
                addFirst(data);
                return;
            }
            if (index==size()){
                addLast(data);
                return;
            }
            ListNode cur=searchIndex(index);
            if (cur!=null){
                cur.prev.next=node;
                node.prev=cur.prev;
                node.next=cur;
                cur.prev=node;
                return;
            }
        }
    
  8. 将链表清除

        public void clear(){
            ListNode cur =head;
            while(cur!=null){
                ListNode nextNode=cur.next;
                cur.prev=null;
                cur.next=null;
                cur=nextNode;
            }
            head=null;
            last=null;
        }
    

面试问题的总结

  1. 顺序表和链表的区别?

  2. 数组和链表的区别?

  3. ArrayList和LinkedList的区别?

    集合框架当中的两个类

    集合框架就是将所有的数据结构,封装成了java自己的类

  4. 技巧:当面试官问到xxxx和xxxx的区别的时候,一般从共同点出发。

    组织:

    1. 顺序表底层是一个数组,逻辑上和物理上都是连续的
    2. 链表是一个有若干节点组成的一个数据结构,逻辑上是连续的,但是在物理上(内存上)是不一定连续的

    操作:

    1. 顺序表适合,查找相关的操作,因为可以使用下标,直接获取某个位置的元素
    2. 链表适合于,频繁的插入和删除操作,此时不需要像顺序表一样,移动元素,只需要修改指向。不支持随机访问
    3. 顺序表不好的的地方,看是否溢出,满了要扩容,扩容之后,不一定就能放完,所以空间利用率不高
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卑微的一条鼠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值