栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java单向链表的模拟实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java单向链表的模拟实现

单向链表是链表的基础,以下为单向链表的模拟实现(需要一定基础)

首先是功能实现,函数成员如下:

//头插法
public void addFirst(int data);
//尾插法
public void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data);
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key);
//删除第一次出现关键字为key的节点
public void remove(int key);
//删除所有值为key的节点
public void removeAllKey(int key);
//得到单链表的长度
public int size();
public void display();
public void clear();
 

接下来我们需要构思一个大的框架来进行代码的编写。

我的思路为:

一个包由三个类组成。

首先是大家比较关注的链表类:

public class MyList {

    public static class ListNode{
      int val;
      public ListNode next;
      public ListNode(int val){
            this.val=val;
        }
    };

    public ListNode head;

    public void createList(){
        ListNode ListNode1=new ListNode(45);
        ListNode ListNode2=new ListNode(45);
        ListNode ListNode3=new ListNode(45);
        ListNode ListNode4=new ListNode(34);

        ListNode1.next=ListNode2;
        ListNode2.next=ListNode3;
        ListNode3.next=ListNode4;
        ListNode4.next=null;

        this.head=ListNode1;
    }

    public void display(){
        
        
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }

    //头插法
    public void addFirst(int data) {
        ListNode node=new ListNode(data);
//        if(head==null){
//            head=node;
//        }
//        else{
//            node.next=head;
//            head=node;
//        }
        node.next=head;
        head=node;

    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
        } else {
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //检查index是否合法
    private void checkindex(int index){
        if(index<0||index>size()){
            throw new IndexOutOfException("位置不合法,请重新输入:");
        }
    }
    //找到index-1位置的节点
    private ListNode FindIndexNode(int index){
        ListNode cur=head;
        while (index-1!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkindex(index);
        if(index==0){
            addFirst(data);
            return;
        }
        else if(index==size()){
            addLast(data);
            return;
        }
        else {
            ListNode node = new ListNode(data);
            ListNode cur=FindIndexNode(index);
            node.next=cur.next;
            cur.next=node;
            }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur=this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur=head;
        while (cur.next!=null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur=cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur=head.next;
        ListNode pre=head;
        while(cur!=null){
            if(cur.val==key){
                pre.next=cur.next;
                cur=cur.next;
            }else{
            pre=cur;
            cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count=0;
        ListNode cur=head;
        while(cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }

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

}

值得一提的是:链表的实质是一个个 由元素(val)+引用(next)的类组成。

注意事项:

1、在编写函数的过程中,首先需要考虑的是head是否为空指针,即原链表中是否已存在元素。

2、遍历的过程中,循环的条件一般为cur.next是否为空指针,因为一旦cur.next为空,则意味着链表走到了最后一个元素。

主函数类部分:

public class main {
    public static void main(String[] args) {
        MyList list=new MyList();
        list.createList();
        list.display();
        System.out.println(list.contains(45));
        System.out.println(list.size());
//        list.addFirst(99);
//        list.display();
//        list.addLast(999);
//        list.display();
        list.addIndex(1,50);
        list.display();
        System.out.println();

        list.removeAllKey(45);
        list.display();

        list.clear();
        list.display();
    }
}

异常类部分:

public class IndexOutOfException extends RuntimeException{
    public IndexOutOfException(){

    }

    public IndexOutOfException(String mes){
        super(mes);
    }
}

有问题可以评论区询问哦。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1006831.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号