数组,队列与链表(详细,单链表的面试题)

稀疏数组

基本介绍

当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。

稀疏数组的处理方法是:

1)记录数组一共有几行几列,有多少个不同的值

2)把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

案例:

1)使用稀疏数组,来保留类似前面的二维数组(棋盘、地图等等)

2)把稀疏数组存盘,并且可以从新恢复原来的二维数组数

3)整体思路分析

在这里插入图片描述

4)代码实现

package com.zhao.sparsearray;

public class SparseArray {
    public static void main(String[] args) {
        //创建一个原始的二维数组 11*11
        //0:表示没有棋子,1表示黑子 2表蓝子
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2]=1;
        chessArr1[2][3]=2;
        chessArr1[5][5]=2;
        chessArr1[4][4]=1;
        //输出原始的二位数组
        System.out.println("原始的二位数组");
        for (int[] row : chessArr1){
            for (int data : row){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

        //将二维数组转稀疏数组的步骤
        //1.先遍历二维数组得到非0数据的个数
        int sum = 0;
        for (int i = 0;i<11;i++){
            for (int j=0;j<11;j++){
                if (chessArr1[i][j] != 0){
                    sum++;
                }
            }
        }

        //2.创建对应的稀疏数组
        int sparseArr[][] = new int[sum+1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum ;

        //遍历二维数组,将非0的值存放到 sparseArr中
        int count = 0;//count,用于记录是第几个非0数据
        for (int i = 0;i<11;i++){
            for (int j = 0;j<11;j++){
                if (chessArr1[i][j] != 0){
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }

        //输出稀疏数组的形式
        System.out.println();
        System.out.println("得到的稀疏数组为-----");
        for (int i=0;i<sparseArr.length;i++){
            System.out.printf("%d\t%d\t%d\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
        }

        //将稀疏数组--》恢复成原始的二维数组
        //1.先读取稀疏数组的第-行,根据第一行的数据,创建原始的二维数组,比如上面的ChessArr 2=int[11][11]
        //2.在读取稀疏数组后几行的数据,并赋给原始的二维数组即可。


        //1。先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

        //2.在读取稀疏数组后几行的数据(从第二行开始),并赋给原始的二维数组即可
        for (int i=1;i<sparseArr.length;i++){
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }

        //输出二维数组
        System.out.println("转换的二位数组");
        for (int[] row : chessArr2){
            for (int data : row){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }


    }
}

队列

介绍:

队列是一个有序列表,可以用数组或是链表来实现。

遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出

使用场景:

​ 银行排队案例

数组模拟队列

队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图,其中maxSize是该队列的最大容量。

因为队列的输出、 输入是分别从前后端来处理,因此需要两个变量front及rear分别记录队列前后端的下标,front 会随着数据输出而改变,而rear则是随着数据输入而改变,如图所示:

在这里插入图片描述

package com.zhao.queue;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' '; //接受用户输入
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;

        while (flag){
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取出数据");
            System.out.println("h(head):查看队列头的数据");
            key = scanner.next().charAt(0);//接受一个字符
            switch (key){
                case 's':
                    queue.show();
                    break;
                case 'e':
                    flag = false;
                    break;
                case 'a':
                    System.out.println("请输入一个数:");
                    int num = scanner.nextInt();
                    queue.addQueue(num);
                    break;
                case 'g':
                    try {
                        int value = queue.getQueue();
                        System.out.println(value);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int value = queue.headQueue();
                        System.out.println(value);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

//使用数组模拟队列 - 编写一个ArrayQueue类
class ArrayQueue{
    private int maxSize;//表示数组的最大容量
    private int front;  //队列头
    private int rear;   //队列尾
    private int[] arr;  //用来存放数据,模拟队列

    //创建队列
    public ArrayQueue(int arrMaxSize){
        this.maxSize = arrMaxSize;
        this.arr = new int[maxSize];
        this.front = -1; //指向队列头部,分析出front是指向队列头的前一个位置
        this.rear = -1;  //指向队列尾,指向队列尾的数据(即就是队列最后一个数据)

    }

    //判断队列是否满
    public boolean isFull(){
        return rear == maxSize-1;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return front == rear;
    }

    //向队列添加数据
    public  void addQueue(int num){
        if (isFull()){
            System.out.println("队列满了");
        }else {
            rear++;
            arr[rear] = num;
        }

    }

    //从队列取数据
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,没有数据");
        }else {
            front++;
            return arr[front];
        }

    }

    //显示队列的所有数据
    public void show(){
        if (isEmpty()){
            System.out.println("队列为空,没有数据");
        }else {
            for (int i=0;i<arr.length;i++){
                System.out.print("第"+(i+1)+"个数据:"+arr[i]);
                System.out.println();
            }
        }
    }

    //显示对列的头数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,没有数据");
        }else {
            return arr[front+1];
        }
    }

}

问题分析并 优化

1)目前数组使用一次就不能用,没有达到复用的效果

2)将这个数组使用算法,改进成一个环形的取模%

数组模拟环形队列

对前面的数组模拟队列的优化,充分利用数组因此将数组看做是一个环形的。

1)尾索引的下一个为头索引时表示队列满,即将队列容量空出一个作为约定,这个在做判断队列满的时候需要注意(rear+ 1)% maxSize == front 满
2)rear== front [空]
3)分析示意图

在这里插入图片描述

代码实现:

package com.zhao.queue;

import java.util.Scanner;

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        System.out.println("测试数组模拟环形队列的案例------");
        CircleArrayQueue queue = new CircleArrayQueue(4);//设置4,有效数据最大为3
        char key = ' '; //接受用户输入
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;

        while (flag){
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取出数据");
            System.out.println("h(head):查看队列头的数据");
            key = scanner.next().charAt(0);//接受一个字符
            switch (key){
                case 's':
                    queue.show();
                    break;
                case 'e':
                    flag = false;
                    break;
                case 'a':
                    System.out.println("请输入一个数:");
                    int num = scanner.nextInt();
                    queue.addQueue(num);
                    break;
                case 'g':
                    try {
                        int value = queue.getQueue();
                        System.out.println(value);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int value = queue.headQueue();
                        System.out.println(value);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}
//使用数组模拟队列 - 编写一个ArrayQueue类
class CircleArrayQueue{
    private int maxSize;//表示数组的最大容量
    //front的初始值= 0
    private int front;  //1. front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说arr[front]就是队列的第一个元素

    //rear的初始值= 0
    private int rear;   //2. rear 变量的含义做一个调整: rear 指向队列的最后一个元素的后一个位置.因为希望空出一个空间做为约定.

    private int[] arr;  //用来存放数据,模拟队列

    //创建队列
    public CircleArrayQueue(int arrMaxSize){
        this.maxSize = arrMaxSize;
        this.arr = new int[maxSize];
        this.front = 0;
        this.rear = 0;
    }

    //判断队列是否满
    public boolean isFull(){
        return (rear+1)%maxSize == front;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return front == rear;
    }

    //向队列添加数据
    public  void addQueue(int num){
        if (isFull()){
            System.out.println("队列满了");
        }else {
            //直接加入数据
            arr[rear] = num;
            //将rear 后移,这里必须考虑取模
            rear = (rear + 1) % maxSize;
        }

    }

    //从队列取数据
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,没有数据");
        }else {
            int value = arr[front];
            front = (front + 1) % maxSize;
            return value;
        }

    }

    //显示队列的所有数据
    public void show(){
        if (isEmpty()){
            System.out.println("队列为空,没有数据");
        }else {
            //思路:从front开始遍历,遍历多少个元素
            //有效数据数:(rear + maxSize - front)%maxSize
            for (int i=front;i<front+size();i++){
                System.out.print("arr["+(i%maxSize)+"]:"+arr[(i%maxSize)]);
                System.out.println();
            }
        }
    }

    public int size(){
        //rear = 1
        //front = 0
        //maxSize = 3
        //有效值 1
        return (rear+maxSize-front)%maxSize;
    }

    //显示对列的头数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,没有数据");
        }else {
            return arr[front];
        }
    }

}

链表(Linked List)

单项链表
链表是有序的列表
  • 链表是以节点的方式来存储,是链式存储
  • 每个节点包含data域,next域: 指向下一个节点.
  • 发现链表的各个节点不一定是连续存储
  • 链表分带头结点的链表,和没有头节点的链表
应用实例

使用带head头的单向链表实现 - 水浒英雄排行榜管理

1)完成对英雄人物的增删改查操作,注: 删除和修改,查找

2)第一种方法在添加英雄时,直接添加到链表的尾部

在这里插入图片描述

3)第二种方式在添加英雄时,根据排名将英雄插入到指定位置(如果有这个排名,则添加失败,并给出提示)

在这里插入图片描述

删除

在这里插入图片描述

代码实现
package com.zhao.linkedlist;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode heroNode1 = new HeroNode(1,"宋江","及时雨");
        HeroNode heroNode2 = new HeroNode(2,"晁盖","托塔天王");
        HeroNode heroNode3 = new HeroNode(3,"卢俊义","玉麒麟");
        HeroNode heroNode4 = new HeroNode(4,"林冲","豹子头");
        HeroNode heroNode5 = new HeroNode(4,"赵童","豹子头");

        SingleLinkedList linkedList = new SingleLinkedList();
        System.out.println("按顺序添加的:");
        linkedList.addByOrder(heroNode1);
        linkedList.addByOrder(heroNode2);
        linkedList.addByOrder(heroNode4);
        linkedList.addByOrder(heroNode3);
        linkedList.list();

        System.out.println("修改后的链表:");
        linkedList.update(heroNode5);
        linkedList.list();

        System.out.println("删除后的链表:");
        linkedList.del(4);
        linkedList.list();
    }
}


//定义SingleLinkedList管理我们的英雄
class SingleLinkedList{
    //先初始化一个头节点,头节点不要动,不存放具体的数据
    private HeroNode head = new HeroNode(0,"","");

    //添加节点到单向链表
    //思路,当不考虑编号顺序时
    //1.找到当前链表的最后节点
    //2.将最后这个节点的next 指向新的节点
    public void add(HeroNode heroNode){
        //因为head节点不能动,因此我们需要一个辅助遍历temp
        HeroNode temp = head;
        //遍历链表,找到最后
        while (true){
            //找到链表的最后
            if (temp.next == null){
                break;
            }
            //如果没找到最后,将temp后移
            temp = temp.next;
        }

        //当退出while循环时,temp就指向了链表的最后
        //将最后这个节点的next指向新的节点
        temp.next = heroNode;

    }

    //第二种方式在添加英雄时,根据排名将英雄插入到指定位置
    //(如果有这个排名,则添加失败,并给出提示)
    public void addByOrder(HeroNode heroNode){
        //因为头节点不能动,因此我们仍然通过-个辅助指针(变量)来帮助找到添加的位置
        //因为单链表,因为我们找的temp是位于添加位置的前一个节点,否则插入不了
        HeroNode temp = head;
        boolean flag = false;//flag 标志添加的编号是否存在,默认为false
        while (true){
            if (temp.next == null){// 说明temp已经在链表的最后
                break;//
            }
            if (temp.next.no > heroNode.no){//位置找到,就在temp的后面插入
                break;

            }else  if (temp.next.no == heroNode.no){//说明已经存在
                flag = true;
                break;
            }

            temp=temp.next;//后移,遍历当前链表
        }

        //判断flag的值
        if (flag){//不能添加,说明编号存在
            System.out.println("编号为"+heroNode.no+"的英雄已经存在了");

        }else {
            //插入到链表中,temp的后面
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }


    //修改节点的信息,根据no编号来修改,即no编号不能改
    //1.根据 newHeroNode 的 no 来修改即可
    public  void update(HeroNode newHeroNode){
        //判断链表是否为空
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }

        //找到需要修改的节点,根据no编号
        //定义一个辅助变量
        HeroNode temp = head.next;
        boolean flag = false;//表示是否找到该节点
        while (true){
            if (temp == null){
                break;//已经遍历完链表
            }
            if (temp.no == newHeroNode.no){
                //找到
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //根据flag 判断是否找到要修改的节点
        if (flag){
            temp.name = newHeroNode.name;
            temp.nickName = newHeroNode.nickName;
        }else {
            System.out.println("没有这位英雄");
        }


    }

    //删除节点
    //思路
    //1.head不能动,因此我们需要一个temp辅助节点找到待删除节点的前一个节点
    //2.说明我们在比较时:,是temp.next.no和需要删除的节点的no比较
    public void del(int no){
        HeroNode temp = head;
        boolean flag = false;//标志是否找到待删除节点时
        while (true){
            if (temp.next == null){//已经到链表的最后
                break;
            }
            if (temp.next.no == no){
                //找到的待删除节点的前一个节点temp
                flag = true;
                break;
            }
            temp = temp.next; // temp后移,遍历
        }
        if (flag){//找到
            //可以删除
            temp.next = temp.next.next;
        }else {
            System.out.println("没有这个英雄");
        }
    }



    //显示链表【遍历】
    public void list(){
        //判断链表是否为空
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        //因为头节点不能动,因此我们需要一个辅助遍历temp
        HeroNode temp = head.next;
        while (true){
            //判断是否到链表最后
            if (temp == null){
                break;
            }

            //输出节点信息
            System.out.println(temp);
            //将temp后移
            temp = temp.next;
        }

    }



}

//定义HeroNode , 每一个HeroNode 对象就是一个节点
class HeroNode{
    public int no;
    public String name;
    public String nickName;
    public  HeroNode next;//指向下一个节点

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

1)求单链表中节点的个数

//方法:获取到单链表的节点的个数(如果是带头节点的链表,需要不统计头节点)
    /**
     * @param head 传入头节点
     * @return int
     */
    public static int getLength(HeroNode head){
        if (head.next == null){//空链表
            return 0;
        }
        //定义一个辅助的变量,我们没有統計头节点
        HeroNode cur = head.next;
        int length = 0;
        while (cur != null){
            length++;
            cur = cur.next;//遍历
        }
        return length;
    }

2)查找单链表中的倒数第k个结点

 //查找单链表中的倒数第k个结点[ 新浪面试题]
    //思路
    //1.编写一个方法,接收head节点,同时接收一个index
    //2. index 表示是倒数第index个节点
    //3.先把链表从头到尾遍历,得到链表的总的长度getLength
    //4.得到size 后,我们从链表的第-个开始遍历(size-index)个,就可以得到
    //5.如果找到了,则返回该节点,否则返null
    public static HeroNode findLastIndexNode(HeroNode head,int index){
        //判断链表为空,返回 null
        if (head.next == null){
            return null;//没有找到
        }
        //第一个遍历得到链表的长度(节点个数)
        int size = getLength(head);
        //第二次遍历 size-index 位置,就是我们倒数的第K个节点
        //做一个index的校验
        if (index <= 0 || index > size){
            return null;
        }
        //先定义辅助变量,for循环定位到倒数的index
        HeroNode cur = head.next;
        for (int i = 1;i <= size-index;i++){
            cur = cur.next;
        }
        return cur;
    }
  1. 单链表的反转
//反转单链表
    public static void reverserList(HeroNode head){
        //如果当前链表为空,或者只有一一个节点,无需反转,直接返回
        if (head.next == null || head.next.next == null){
            return;
        }
        //定义一个辅助变量,帮助我们遍历原来的链表
        HeroNode temp = head.next;
        HeroNode next = null;//指向当前节点的下一个节点
        HeroNode reverseHead = new HeroNode(0,"","");
        //遍历原来的链表,每遍历一个节点,就将其取出,并放在新的链表reverseHead的最前端
        while (temp != null){
            next = temp.next;//先暂时保存当前节点的下一个节点,因为后面需要使用
            temp.next = reverseHead.next;//将cur的下一个节点指向新的链表的最前端
            reverseHead.next = temp;
            temp = next;//后移
        }
        //将head . next 指向reverseHead.next ,实现单链表的反转
        head.next = reverseHead.next;
    }

4)从尾到头打印单链表[百度,要求方式1:反向遍历。方式2: Stack栈]

 //方式二:
    //可以利用栈这个数据结构,将各个节点压入到栈中,然后利用栈的先进后出的特点,就实现了逆序打印的效果
    public static void reversePrint(HeroNode head){
        if (head.next == null){
            return;
        }

        Stack<HeroNode> stack = new Stack<>();
        HeroNode temp = head.next;
        while (temp != null){
            stack.push(temp);
            temp = temp.next;
        }

        while (stack.size()>0){
            System.out.println(stack.pop());
        }
    }

5)合并两个有序的单链表,合并之后的链表依然有序[课后练习.]

双向链表

使用带head头的双向链表实现-水浒英雄排行榜

管理单向链表的缺点分析:

1)单向链表, 查找的方向只能是-一个方向,而双向链表可以向前或者向后查找。

2)单向链表不能自我删除,需要靠辅助节点,而双向链表,则可以自我删除,所以前面我们单链表删除
链表,则可以自我删除,所以前面我们单链表删除时节点,总是找到temp,temp是待删除节点的前一
个节点

3)示意图

在这里插入图片描述

4)代码实现

package com.zhao.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList linkedList = new DoubleLinkedList();
        HeroNode2 heroNode1 = new HeroNode2(1,"宋江","及时雨");
        HeroNode2 heroNode2 = new HeroNode2(2,"晁盖","托塔天王");
        HeroNode2 heroNode3 = new HeroNode2(3,"卢俊义","玉麒麟");
        HeroNode2 heroNode4 = new HeroNode2(4,"林冲","豹子头");
        HeroNode2 heroNode6 = new HeroNode2(5,"林冲","豹子头");
        HeroNode2 heroNode7 = new HeroNode2(6,"林冲","豹子头");
        HeroNode2 heroNode5 = new HeroNode2(4,"公孙胜","入云龙");

        linkedList.addByOrder(heroNode3);
        linkedList.addByOrder(heroNode2);
        linkedList.list();
        linkedList.del(2);
        linkedList.list();


    }
}

//创建一个双向链表的类
class DoubleLinkedList{
    private HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }

    //遍历双向链表的方法
    public void list() {
        //判断链表是否为空
        if (head.next == null) {
            return;
        }
        HeroNode2 temp = head.next;

        while (true){
            if (temp == null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }

    }

    //添加
    public void add(HeroNode2 heroNode){
        //因为head节点不能动,因此我们需要一个辅助遍历temp
        HeroNode2 temp = head;
        //遍历链表,找到最后
        while (true){
            //找到链表的最后
            if (temp.next == null){
                break;
            }
            //如果没找到最后,将temp后移
            temp = temp.next;
        }

        //当退出while循环时,temp就指向了链表的最后
        //将最后这个节点的next指向新的节点
        temp.next = heroNode;
        heroNode.pre = temp;

    }

    //第二种方式在添加英雄时,根据排名将英雄插入到指定位置
    //(如果有这个排名,则添加失败,并给出提示)
    public void addByOrder(HeroNode2 heroNode){
        //因为头节点不能动,因此我们仍然通过一个辅助指针(变量)来帮助找到添加的位置
        //因为单链表,因为我们找的temp是位于添加位置的前一个节点,否则插入不了
        HeroNode2 temp = head;
        boolean flag = false;//flag 标志添加的编号是否存在,默认为false
        while (true){
            if (temp.next == null){// 说明temp已经在链表的最后
                break;//
            }
            if (temp.next.no > heroNode.no){//位置找到,就在temp的后面插入
                break;

            }else  if (temp.next.no == heroNode.no){//说明已经存在
                flag = true;
                break;
            }

            temp=temp.next;//后移,遍历当前链表
        }

        //判断flag的值
        if (flag){//不能添加,说明编号存在
            System.out.println("编号为"+heroNode.no+"的英雄已经存在了");

        }else {
            // 为防止出现空指针的情况,需要对temp节点位置进行判断
            // 若双向链表尚未到达尾端,则需要将node节点与其相邻的后面的节点进行连接
            if(temp.next != null) {
                heroNode.next = temp.next;
                temp.next.pre = heroNode;
            }

            // 无论双向链表是否到达尾端,都需要将node节点与其相邻的前面的节点进行连接
            temp.next = heroNode;
            heroNode.pre = temp;

        }
    }


    //修改节点的信息,根据no编号来修改,即no编号不能改
    //1.根据 newHeroNode 的 no 来修改即可
    public void update(HeroNode2 newHeroNode){
        //判断链表是否为空
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }

        //找到需要修改的节点,根据no编号
        //定义一个辅助变量
        HeroNode2 temp = head.next;
        boolean flag = false;//表示是否找到该节点
        while (true){
            if (temp == null){
                break;//已经遍历完链表
            }
            if (temp.no == newHeroNode.no){
                //找到
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //根据flag 判断是否找到要修改的节点
        if (flag){
            temp.name = newHeroNode.name;
            temp.nickName = newHeroNode.nickName;
        }else {
            System.out.println("没有这位英雄");
        }


    }

    //删除节点
    //思路
    //1.head不能动,因此我们需要一个temp辅助节点找到待删除节点的前一个节点
    //2.说明我们在比较时:,是temp.no和需要删除的节点的no比较
    public void del(int no){
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;//标志是否找到待删除节点时
        while (true){
            if (temp == null){//已经到链表的最后
                break;
            }
            if (temp.no == no){
                //找到的待删除节点
                System.out.println("删除了编号为"+no+"的人");
                flag = true;
                break;
            }
            temp = temp.next; // temp后移,遍历
        }

        if (flag){//找到
            //可以删除
            temp.pre.next = temp.next;
            if (temp.next != null){
                temp.next.pre = temp.pre;
            }
        }else {
            System.out.println("没有这个英雄");
        }
    }
}

//定义HeroNode,每一个HeroNode 对象就是一个节点
class HeroNode2{
    public int no;
    public String name;
    public String nickName;
    public  HeroNode2 next;//指向下一个节点
    public  HeroNode2 pre;//指向上一个节点

    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值