iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >数据结构(Java实现)LinkedList与链表(上)
  • 939
分享到

数据结构(Java实现)LinkedList与链表(上)

数据结构java链表 2023-08-30 10:08:53 939人浏览 独家记忆
摘要

链表 逻辑结构 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。 无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。


链表
在这里插入图片描述
逻辑结构
在这里插入图片描述


在这里插入图片描述


无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。
无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。


链表的实现
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

创建一个链表
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


遍历单链表
在这里插入图片描述
在这里插入图片描述


得到单链表的长度 链表中节点的个数
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


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

在这里插入图片描述

在这里插入图片描述


头插和尾插
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


任意位置的插入
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


删除第一次出现关键字为key的节点
在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述


删除所有值为key的节点
在这里插入图片描述

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

在这里插入图片描述


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

在这里插入图片描述


上述单链表的所有代码
MySingleList

public class MySingleList {    class Listnode{        public int val;        public ListNode next;        public ListNode(int val) {            this.val = val;        }    }    public ListNode head;//一个特殊的节点,头节点    //创建一个链表    public void createList(){        ListNode node1=new ListNode(23);        ListNode node2=new ListNode(3);        ListNode node3=new ListNode(253);        ListNode node4=new ListNode(88);        ListNode node5=new ListNode(56);        ListNode node6=new ListNode(23);        node1.next=node2;        node2.next=node3;        node3.next=node4;        node4.next=node5;        node5.next=node6;        this.head=node1;    }    //遍历单链表    public void show(){        ListNode cur=head;        while(cur!=null){            System.out.print(cur.val+" ");            cur=cur.next;        }        System.out.println();    }    //链表的长度    public int size(){        int count=0;        ListNode cur=head;        while(cur!=null){            count++;            cur=cur.next;        }        return count;    }    //查找是否包含关键字key是否在单链表当中    public boolean contains(int key){        ListNode cur=head;        while(cur!=null){            if(cur.val==key){                return true;            }            cur=cur.next;        }        return false;    }    //头插    public void addFirst(int data){        ListNode node=new ListNode(data);        //链表为空的情况下也是成立的        node.next=head;        head=node;    }    //尾插    public void addLast(int data){        ListNode node=new ListNode(data);        if(head==null){            head=node;            return;        }        //不能使用while(cur!=null),当cur==null时,说明已经错过了最后一个节点        //使用下面这种        ListNode cur=head;        while(cur.next!=null){//这里会漏掉空链表的情况,我们在上面一步补充            cur=cur.next;        }        cur.next=node;    }    //任意位置插入(后面插入)    public void addIndex(int index,int data){        int len=size();        //判断index位置的合法性        if(index<0||index>len){            throw new IndexOut("任意位置插入,坐标不合法");        }        if(index==0){            addFirst(data);            return;        }        ListNode node=new ListNode(data);        //一般情况,找到这个index节点的位置        ListNode cur=findIndex(index);        node.next=cur.next;        cur.next=node;    }    private  ListNode findIndex(int index){        ListNode cur=head;        while(index-1!=0){            cur=cur.next;            index--;        }        return cur;//第index位置的节点    }    //删除第一次出现关键字为key的节点    public void remove(int key){        if(head==null){            return;        }        if(head.val==key){//因为在searchPrev中的判断条件为prev.next==null,忽略掉了头节点,这里补充判断            head=head.next;            return;        }        ListNode prev=searchPrev(key);        if(prev==null){            System.out.println("没有这个数据");            return;        }        ListNode del=prev.next;        prev.next=del.next;    }    private ListNode searchPrev(int key){        ListNode prev =head;        while(prev.next!=null){//这里将会使用双指针,prev为待删除节点的前一个节点,因此判断条件要使用prev.next==null,而不是prev==null            if(prev.next.val==key){//上面的条件为遍历链表,这里的条件为判断条件                return prev;            }else{                prev=prev.next;            }        }        return null;    }    //删除所有值为key的节点    public void removeAllKey(int key){        if(head==null){            return;        }        ListNode cur=head.next;        ListNode prev=head;        while(cur!=null){            if(cur.val==key){                prev.next=cur.next;                cur=cur.next;            }else{                prev=cur;                cur=cur.next;            }        }        if(head.val==key){//观察到前面的cur为head.next,漏掉了对head的val值的判断,这里补充上            head=head.next;        }    }    //clear    public void clear(){        this.head=null;    }}

IndexOut

public class IndexOut extends RuntimeException {    public IndexOut() {    }    public IndexOut(String message) {        super(message);    }}

Test1

public class Test1 {    public static void main(String[] args) {        MySingleList mySingleList=new MySingleList();        mySingleList.addFirst(23);        mySingleList.addFirst(2);        mySingleList.addFirst(3);        mySingleList.addFirst(230);        mySingleList.show();        mySingleList.addLast(-13);        mySingleList.addLast(-178);        mySingleList.show();        MySingleList mySingleList1=new MySingleList();        mySingleList1.addLast(45);        mySingleList.show();        mySingleList.clear();        mySingleList.show();        System.out.println("#######");    }}

来源地址:https://blog.csdn.net/qq_43570634/article/details/132516341

--结束END--

本文标题: 数据结构(Java实现)LinkedList与链表(上)

本文链接: https://www.lsjlt.com/news/382546.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作