当前位置: 首页 > news >正文

手动实现LinkedList

前言

大家好,我是Maybe。最近在学习数据结构中的链表,自己手动实现了一个LinkedList。我想与大家分享一下。

思维导图

代码部分

package Constant;public class constant {public static final String INDEX_IS_WRONG="输入的下标不合法";
}
package utils;public class IndexException extends RuntimeException{public IndexException() {super();}public IndexException(String message) {super(message);}
}
public interface IList {// 头插法public void addFirst(int data);// 尾插法public void addLast(int data);// 任意位置插入,第一个数据节点为0号下标public void 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();
}
import Constant.constant;
import utils.IndexException;public class LinkedList implements IList {//1.定义一个内部类static class ListNode{public int val;//类型写成了String,应该是ListNode的public ListNode prev;public ListNode next;public ListNode(int val) {this.val = val;}}public ListNode head;//这个就要给个尾了public ListNode last;@Overridepublic void addFirst(int data) {if(head==null){ListNode node=new ListNode(data);head=last=node;}else{ListNode node=new ListNode(data);node.next=head;head.prev=node;head=node;}}@Overridepublic void addLast(int data) {if(head==null){ListNode node=new ListNode(data);//写成了node=last=null了head=last=node;}else{ListNode node=new ListNode(data);last.next=node;node.prev=last;//这里要注意last=last.next;}}@Override//在LinkedList中找到对应的cur,然后再cur之前插入public void addIndex(int index, int data) {int len=size();if(index<0||index>len){String msg= constant.INDEX_IS_WRONG+index;throw new IndexException(msg);}if(index==0){addFirst(data);}else if(index==len){addLast(data);}else{ListNode node=new ListNode(data);ListNode cur=findIndex(index);node.next=cur;cur.prev.next=node;node.prev=cur.prev;cur.prev=node;}}//在LinkedList中找到对应的curprivate ListNode findIndex(int index){if(head==null){return null;}else{ListNode cur=head;while(index!=0){cur=cur.next;index--;}return cur;}}@Overridepublic boolean contains(int key) {if(head==null){return false;}else{ListNode cur=head;while(cur!=null){if(cur.val==key){return true;}else{cur=cur.next;}}return false;}}@Overridepublic void remove(int key) {//1.先判断链表是否为空if(head==null){return;}else{ListNode cur=head;while(cur!=null){if(cur.val==key){//1.考虑头删的情况if(cur==head){head=head.next;//考虑如果链表中只有一个节点的情况if(head!=null){head.prev=null;}}else{cur.prev.next=cur.next;//尾巴节点和中间节点共用if(cur.next==null){//尾节点last=last.prev;}else{//中间节点cur.next.prev=cur.prev;}}return;}cur=cur.next;}}}@Overridepublic void removeAllKey(int key) {if(head==null){return;}else{ListNode cur=head;while(cur!=null){if(cur.val==key){if(cur==head){head=head.next;//只有一个节点的情况要考虑if(head!=null){head.prev=null;}}else{cur.prev.next=cur.next;if(cur.next==null){last=last.next;}else{cur.next.prev=cur.prev;}}}cur=cur.next;}}}@Overridepublic int size() {if(head==null){return 0;}else{ListNode cur=head;int count=0;while(cur!=null){cur=cur.next;count++;}return count;}}@Overridepublic void display() {if(head==null){return;}else{ListNode cur=head;while(cur!=null){System.out.print(cur.val+" ");cur=cur.next;}System.out.println();}}@Overridepublic void clear() {if(head==null){return;}else{ListNode cur=head;while(cur!=null){ListNode curN=cur.next;cur.prev=null;cur.next=null;cur=curN;}head=last=null;//最后要把head和last置为null}}
}
import utils.IndexException;public class Test {public static void main(String[] args) {LinkedList linkedList=new LinkedList();linkedList.addLast(1);linkedList.addLast(1);linkedList.addLast(1);linkedList.addLast(2);linkedList.display();
//        linkedList.addFirst(1);
//        linkedList.addFirst(2);
//        linkedList.addFirst(3);
//        linkedList.addFirst(4);
//        linkedList.display();
//        int ret=linkedList.size();
//        System.out.println(ret);
//        try{
//            linkedList.addIndex(2,100);
//
//        }catch (IndexException e){
//            e.printStackTrace();
//        }
//        linkedList.display();
//        linkedList.remove(1);
//        linkedList.display();
//        linkedList.removeAllKey(1);linkedList.clear();linkedList.display();}
}

结语 

本次分享到此结束啦。希望可以帮到有需要的人!

 

 

 

 

 

 

相关文章:

  • 【算法数据结构】leetcode37 解数独
  • Unreal 从入门到精通之如何接入MQTT
  • 代码审计入门 原生态sql注入篇
  • 事件冒泡与捕获
  • LeetCode 438 找到字符串中所有字母异位词
  • C语言学习之预处理指令
  • 定制一款国密浏览器(9):SM4 对称加密算法
  • 微信小程序 时间戳与日期格式的转换
  • 今天分享一个网店客服回复数据集-用于网点客服AI助手自动回复智能体训练
  • 下采样(Downsampling)
  • python文件处理自用
  • 【PCIE配置空间】
  • 软件中的保护锁在工程项目中的应用
  • C算术运算符 printf输出格式 字符指针打印输出 使用scanf函数进行输入
  • MCGS昆仑通太屏笔记
  • 【mongodb】数据库操作
  • OSI七层网络模型详解
  • 【MySQL】MySQL建立索引不知道注意什么?
  • OpenStack Yoga版安装笔记(23)Swift安装
  • 六边形棋盘格(Hexagonal Grids)的坐标
  • 澳门世界杯“中日对决”,蒯曼击败伊藤美诚晋级女单决赛
  • 圆桌|艺术院校校长怎么看AI时代的艺术教育
  • 东南亚三国行第三日|中马将在人工智能、大熊猫保护、铁路等多领域深化合作
  • 伊朗外交部:美矛盾立场被视为缺乏谈判的严肃性和诚意
  • 海南陵水县一别墅区被指违建已获确认,60岁举报人曾两度遭人蒙面袭击
  • 国家统计局:今年一季度新质生产力加快培育壮大