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


链表

逻辑结构



无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。

无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。


链表的实现


创建一个链表


遍历单链表



得到单链表的长度 链表中节点的个数


查找是否包含关键字key是否在单链表当中


头插和尾插


任意位置的插入


删除第一次出现关键字为key的节点



删除所有值为key的节点



clear


上述单链表的所有代码

MySingleList

java 复制代码
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;
        }
/*        if(index==len){
            addLast(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

java 复制代码
public class IndexOut extends RuntimeException {
    public IndexOut() {
    }

    public IndexOut(String message) {
        super(message);
    }
}

Test1

java 复制代码
public class Test1 {
    public static void main(String[] args) {
        MySingleList mySingleList=new MySingleList();
/*        mySingleList.createList();

        mySingleList.show();
        System.out.println(mySingleList.size());
        System.out.println(mySingleList.contains(100));
        System.out.println(mySingleList.contains(23));
        mySingleList.show();*/
        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);/*
        mySingleList1.show();

        mySingleList.addIndex(3,999);
        try{
            mySingleList.addIndex(4,999);
        } catch (IndexOut e) {
            e.printStackTrace();
        }
        mySingleList.addIndex(mySingleList.size(), 56);*/

/*        mySingleList.show();
        mySingleList.remove(88);
        mySingleList.remove(23);
        mySingleList.show();
        mySingleList.remove(230);*/
/*        mySingleList.show();
        mySingleList.addIndex(3,33);
        mySingleList.addIndex(5,33);
        mySingleList.show();
        mySingleList.removeAllKey(33);
        mySingleList.show();

        mySingleList.addIndex(0,33);
        mySingleList.show();
        mySingleList.removeAllKey(33);*/
        mySingleList.show();
        mySingleList.clear();
        mySingleList.show();
        System.out.println("#######");




    }
}
相关推荐
manba_7 分钟前
leetcode-560. 和为 K 的子数组
数据结构·算法·leetcode
是梦终空12 分钟前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
落落落sss22 分钟前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
码爸25 分钟前
flink doris批量sink
java·前端·flink
.普通人39 分钟前
c语言--力扣简单题目(回文链表)讲解
c语言·leetcode·链表
汉字萌萌哒1 小时前
【2022 CCF 非专业级别软件能力认证第一轮(CSP-J1)入门级 C++语言试题及解析】
数据结构·c++·算法
th新港1 小时前
CCF201909_1
数据结构·c++·算法·ccf
Monodye1 小时前
【Java】网络编程:TCP_IP协议详解(IP协议数据报文及如何解决IPv4不够的状况)
java·网络·数据结构·算法·系统架构
pzx_0011 小时前
【内积】内积计算公式及物理意义
数据结构·python·opencv·算法·线性回归
一丝晨光1 小时前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符