数据结构-单向链表

首先,我们要知道一个前提,java里面是没有指针的概念的,我们是用对象来模拟指针

链表的优缺点,为什么要使用链表?

链表是一个动态数据结构,因此它可以在运行时通过分配和取消分配内存来增长和收缩。 因此,无需给出链表的初始大小。(不必初始化大小,动态)

单链表节点的插入和删除非常容易。 与数组不同,在插入或删除元素后,我们不必移动元素。 在链表中,我们只需要更新节点的下一个指针中存在的地址即可。(增删快,查询慢)

由于链表的大小可以在运行时增加或减小,因此不会浪费内存。 在数组的情况下,会浪费大量内存,例如,如果我们声明一个大小为10的数组并在其中仅存储6个元素,那么就会浪费4个元素的空间。 链表中没有这种问题,因为仅在需要时才分配内存。(动态,所用即所用,无内存浪费)

与数组相比,在链表中存储元素需要更多的内存。 因为在链表中,每个节点都包含一个指针,并且它本身需要额外的内存。(对象指针需要额外的内存)

在链表中很难遍历元素或节点。 我们不能像按索引在数组中那样随机访问任何元素.例如,如果我们要访问位置n处的节点,则必须遍历它之前的所有节点.因此,访问节点所需的时间很大.(查询慢)

在链表中,反向遍历确实很困难。 在双链表的情况下,它比较容易,但是向后指针需要额外的内存,因此浪费了内存。

单链表的存储结构:

小结上图:

1)链表是以节点的方式来存储,是链式存储
2)每个节点包含 data 域, next域:指向下一个节点

3)如图:发现链表的各个节点不一定是连续存储
4)链表分带头节点的链表没有头节点的链表,根据实际的需求来确定

单链表(带头结点)逻辑结构示意图如下

用JAVA代码来实现单链表(带头节点)的增删

public class HeroNodeTest {
    public static void main(String[] args) {
        HeroNode heroNode1=new HeroNode(1,"小民","大帅哥");
        HeroNode heroNode2=new HeroNode(2,"小红","大帅哥2");

        SingleNode singleNode=new SingleNode();
        singleNode.add(heroNode1);
        singleNode.add(heroNode2);

        singleNode.list();

    }

}


class SingleNode{
    //初始化头节点
    private final HeroNode head=new HeroNode(0,"","");


    //定义添加节点到单向链表的方法
    //步骤:不考虑编号的情况下
    //1.找到当前链表的最后节点
    //2.将最后一个节点的next指向新的节点
    public  void add(HeroNode heroNode){

        //定义一个辅助指针找到末节点
        HeroNode temp=head;      //从头开始找
        while(temp.next!=null){
            temp=temp.next;       //注意next是节点对象
        }
        temp.next=heroNode;       //将新增的节点添加到最后一个节点的next
    }

    //遍历列表
    public  void list(){
        if(head.next==null){
            System.out.println("链表为空");
        }

        //定义一个辅助指针找到末节点
        HeroNode temp=head;      //从头开始找
        while ((temp.next!=null)){
            System.out.println(temp.next);
            temp=temp.next; //节点后移
        }

    }

}

//节点结构
class HeroNode{
    public  int no;          //data
    public  String name;       //data
    public  String nickname;   //data
    public  HeroNode next;     //用对象模拟指针,来指向下一个节点 next

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

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

HeroNode{no=1, name='小民', nickname='大帅哥'}
HeroNode{no=2, name='小红', nickname='大帅哥2'}
 

按照编号加入节点

 //按顺序添加节点
    public void addByOrder(HeroNode heroNode){
        //定义一个辅助指针找到末节点
        HeroNode temp=head;      //从头开始找
        boolean flag = false;      //标识符
        while(true){
            if(temp.next==null){
                break;
            }
            if(temp.next.no>heroNode.no){

               break;
            }
            else  if(temp.next.no==heroNode.no){
                flag=true;     //节点已经存在
                break;
            }
            temp=temp.next;//temp后移
        }

        if(flag){
            System.out.println("插入节点已经存在!");
        }
        else {
            heroNode.next=temp.next;   //插入节点,节点next指向temp的next
            temp.next=heroNode;           //temp的next存入节点
        }

    }

删除节点 

  //删除节点
    //单向链表一定要找到删除节点的前一个节点
    //被删除的节点不会再被引用,会被JVM自动回收
    public  void  delete(int no){
        //定义一个辅助指针找到末节点
        HeroNode temp=head;
   
        while (temp.next!=null){
            if(temp.next.no==no){
                temp.next=temp.next.next;
                break;
            }
            temp=temp.next;

        }
    }

获取节点的个数

   //获取节点个数
    public  int getIndexSum(){
        int num=0;
        if(head.next==null){
            System.out.println("链表为空");
        }

        //定义一个辅助指针找到末节点
        HeroNode temp=head;      //从头开始找
        while ((temp.next!=null)){
            num++;
            temp=temp.next; //节点后移
        }
        return num;
    }

单链表反转 (剑指offer24)

    //反转链表
    //方法:头插法
    //定义一个新的节点,遍历链表,每次都将链表放到新节点的最前端
    public void reverseNode(HeroNode heroNode) {
        if (head.next == null || head.next.next == null)
            return;         //当前链表为空或者链表只有一个节点,无需反转


        //定义一个辅助的指针,帮助我们遍历原来的链表
        HeroNode temp = head.next;
        HeroNode tempNext = null;   //指向当前temp节点的下一个节点,相当于一个暂存空间
        HeroNode reverseHead = new HeroNode(0, "", "");
        //遍历链表,每次遍历的节点放到reverseHead的前端
        while (temp != null) {
            tempNext=temp.next;   //暂存当前节点的下一个节点
            temp.next=reverseHead.next;//将原链表的下一个节点指向新的链表的最前端
            reverseHead.next=temp;//将原链表的首节点插入到reversHead节点的后面
            temp=tempNext;   //temp后移
        }
       head.next=reverseHead.next;

    }

 扩展:双指针迭代法

//迭代法  转自LeetCode
//时间复杂度:O(n),其中 n 是链表的长度。需要遍历链表一次。
//空间复杂度:O(1)。
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}

递归法

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}

从尾到头打印单链表(剑指 Offer 06)

    //从尾到头打印单链表,不破坏链表结构
    //方法:栈
    public  void printReverse(HeroNode heroNode){
        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());  //出栈
       }

    }

再次强调一下,我这里所有的方法都是有自己定义一个虚拟头节点来操作的!!!! 

这里我们会发现,我们学过的集合其实就是对链表的一种封装 ,集合底层用到了很多数据结构,这里可以带一下,如List接口中的LinkedList底层就是双向链表,ArrayList是数组

所以学好数据结构才能知道那些我们常用Api的使用场景和各自的优点。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JagTom

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

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

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

打赏作者

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

抵扣说明:

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

余额充值