数据结构(更新至链表)

数组

数组的代码

public class Myarr<T> {private int capacity=0;private int size=0;private T[]arr;//如果是空的则强制将容量扩充为10public Myarr(int capacity) {if (this.size == 0) {this.capacity = 10;this.arr=(T[]) new Object[this.capacity];}}//按顺序添加数据public void add(T value){if(this.size>=capacity){T[]arr1=Arrays.copyOf(arr,2*this.capacity);this.arr=arr1;this.capacity=this.capacity*2;}arr[this.size++]=value;}//按下标删除数据public void delete(int index){if(index>=this.size||index<0){System.out.println("输入有误");return;}if((this.size<=capacity/4)&&(capacity/4>0)){T []arr1=Arrays.copyOf(arr,this.capacity/4);this.arr=arr1;this.capacity=this.capacity/4;}for(int i=index+1;i<this.size;i++){arr[i-1]=arr[i];}this.size--;}//查找指定下标的数据public void search(int index){if(index>=this.size||index<0){System.out.println("输入有误");return;}System.out.println(index+"处的值为:"+arr[index]);}//修改指定下标的值public void modify(int index,T value){if(index>=this.size||index<0){System.out.println("输入有误");return;}arr[index]=value;}//将数组转为字符串输出@Overridepublic String toString() {StringBuffer stringBuffer=new StringBuffer();stringBuffer.append('[');for(int i=0;i<this.size;i++){stringBuffer.append(arr[i]);if(i!=this.size-1){stringBuffer.append(",");}}stringBuffer.append("]");return stringBuffer.toString();}//向数组指定位置添加数值public void aimedAdd(int index,T value){if(index<0||index>this.capacity){throw new IllegalArgumentException("索引越界!");}if(this.size>=capacity){T []arr1=Arrays.copyOf(arr,2*this.capacity);this.arr=arr1;this.capacity=this.capacity*2;}//当添加的数值大于数组实际长度时将进行扩容this.size++;for(int i=this.size;i>index;i--){arr[i]=arr[i-1];}arr[index]=value;}//获取数组长度public int getsize(){return this.size;}//获取数组容积public int getCapacity(){return this.capacity;}public T remove(){if((this.size<=capacity/4)&&(capacity/4>0)){T []arr1=Arrays.copyOf(arr,this.capacity/4);this.arr=arr1;this.capacity=this.capacity/4;}//当数组实际使用长度小于容积的四分之一时将对容积进行缩容arr[size]=null;this.size--;return arr[size];}public T gethead(){return arr[size-1];}
}

链表

链表的代码

public class MyLinkList<T> {class Node<T>{//使用内部类创建节点T data;//节点本身的数据Node next;//指向下一个节点public Node(T data,Node node){//内部类的构造this.data=data;this.next=node.next;}public Node(T data){this.data=data;this.next=null;}}//链表相关的属性和方法private Node head;//链表的头结点private int size;public MyLinkList(){this.head=new Node(null);this.size=0;}//判断链表是否为空public boolean isEmpty(){return this.size==0;}//头部插入元素public void addHead(T data){//1.创建结点Node node=new Node(data);node.next=node;//2.挂接node.next=this.head;this.head=node;//3.更新sizethis.size++;}public void addTail(T data){Node node=new Node(data);Node curnode=this.head;if(this.head==null){this.head=node;}else {while (curnode.next!=null){curnode=curnode.next;}curnode.next=node;}this.size++;}//获取链表大小public int getsize(){return this.size;}// 重写toString 打印链表中的数据@Overridepublic String toString(){//从头结点开始向后遍历Node curnode=this.head;StringBuffer stringBuffer=new StringBuffer();while(curnode!=null){stringBuffer.append(curnode.data+"--->");curnode=curnode.next;}stringBuffer.append("null");return stringBuffer.toString();}//根据索引获得链表的值public T getByIndex(int index){//1.判断索引是否有效if(index<0||index>this.size){throw new IllegalArgumentException("索引无效");}Node<T> curNode=this.head.next;int i=0;while(i<index){curNode=curNode.next;i++;}return curNode.data;}//从链表头部删除元素public T deleteHead(){if(this.isEmpty()){return null;}Node<T> delNode=this.head.next;this.head.next=delNode.next;delNode.next=null;this.size--;return delNode.data;}//从链表尾部删除元素public T deletetail(){if (this.isEmpty()){return null;}Node<T> pre=this.head;while(pre.next.next!=null){pre=pre.next;}this.size--;Node<T> delNode=pre.next;pre.next=delNode.next;delNode.next=null;return delNode.data;}//在链表任意位置删除public T deleteByIndex(int index){int count=1;if(index<0||index>this.size){return null;}Node<T> pre=this.head;while(pre.next!=null) {pre = pre.next;count++;if (count == index) {break;}}Node<T> delNode=pre.next;pre.next=delNode.next;delNode.next=null;return delNode.data;}//根据值在链表中删除元素public int removeByValue(T value){int count=0;//定义前驱结点Node<T> pre=this.head;while(pre.next!=null){Node<T> curNode=pre.next;if(curNode.data.equals(value)){pre.next=pre.next.next;curNode.data=null;this.size--;count++;}else{pre=pre.next;}}return count;}
}

  • 线性结构

  • 数据只能从栈顶进,栈顶出

    图示

基于数组实现的栈

数组的代码:

public class Myarr<T> {private int capacity=0;private int size=0;private T[]arr;//如果是空的则强制将容量扩充为10public Myarr(int capacity) {if (this.size == 0) {this.capacity = 10;this.arr=(T[]) new Object[this.capacity];}}//按顺序添加数据public void add(T value){if(this.size>=capacity){T[]arr1=Arrays.copyOf(arr,2*this.capacity);this.arr=arr1;this.capacity=this.capacity*2;}arr[this.size++]=value;}//按下标删除数据public void delete(int index){if(index>=this.size||index<0){System.out.println("输入有误");return;}if((this.size<=capacity/4)&&(capacity/4>0)){T []arr1=Arrays.copyOf(arr,this.capacity/4);this.arr=arr1;this.capacity=this.capacity/4;}for(int i=index+1;i<this.size;i++){arr[i-1]=arr[i];}this.size--;}//查找指定下标的数据public void search(int index){if(index>=this.size||index<0){System.out.println("输入有误");return;}System.out.println(index+"处的值为:"+arr[index]);}//修改指定下标的值public void modify(int index,T value){if(index>=this.size||index<0){System.out.println("输入有误");return;}arr[index]=value;}//将数组转为字符串输出@Overridepublic String toString() {StringBuffer stringBuffer=new StringBuffer();stringBuffer.append('[');for(int i=0;i<this.size;i++){stringBuffer.append(arr[i]);if(i!=this.size-1){stringBuffer.append(",");}}stringBuffer.append("]");return stringBuffer.toString();}//向数组指定位置添加数值public void aimedAdd(int index,T value){if(index<0||index>this.capacity){throw new IllegalArgumentException("索引越界!");}if(this.size>=capacity){T []arr1=Arrays.copyOf(arr,2*this.capacity);this.arr=arr1;this.capacity=this.capacity*2;}this.size++;for(int i=this.size;i>index;i--){arr[i]=arr[i-1];}arr[index]=value;}//获取数组长度public int getsize(){return this.size;}//获取数组容积public int getCapacity(){return this.capacity;}public T remove(){if((this.size<=capacity/4)&&(capacity/4>0)){T []arr1=Arrays.copyOf(arr,this.capacity/4);this.arr=arr1;this.capacity=this.capacity/4;}arr[size]=null;this.size--;return arr[size];}public T gethead(){return arr[size-1];}
}

数组实现的栈的代码:

public class ArrStack<T> implements Mystack<T>{private Myarr<T> data;public ArrStack(Myarr data) {this.data = data;}@Overridepublic void push(T value) {this.data.add(value);}@Overridepublic T pop() {return this.data.remove();}@Overridepublic T peek() {return this.data.gethead();}@Overridepublic boolean IsEmpty() {if(this.data.getsize()==0){return true;}else{return false;}}
}

基于链表实现的栈

链表的代码

public class MyLinkList<T> {class Node<T>{//使用内部类创建节点T data;//节点本身的数据Node next;//指向下一个节点public Node(T data,Node node){//内部类的构造this.data=data;this.next=node.next;}public Node(T data){this.data=data;this.next=null;}}//链表相关的属性和方法private Node head;//链表的头结点private int size;public MyLinkList(){this.head=new Node(null);this.size=0;}//判断链表是否为空public boolean isEmpty(){return this.size==0;}//头部插入元素public void addHead(T data){//1.创建结点Node node=new Node(data);node.next=node;//2.挂接node.next=this.head;this.head=node;//3.更新sizethis.size++;}public void addTail(T data){Node node=new Node(data);Node curnode=this.head;if(this.head==null){this.head=node;}else {while (curnode.next!=null){curnode=curnode.next;}curnode.next=node;}this.size++;}//获取链表大小public int getsize(){return this.size;}// 重写toString 打印链表中的数据@Overridepublic String toString(){//从头结点开始向后遍历Node curnode=this.head;StringBuffer stringBuffer=new StringBuffer();while(curnode!=null){stringBuffer.append(curnode.data+"--->");curnode=curnode.next;}stringBuffer.append("null");return stringBuffer.toString();}//根据索引获得链表的值public T getByIndex(int index){//1.判断索引是否有效if(index<0||index>this.size){throw new IllegalArgumentException("索引无效");}Node<T> curNode=this.head.next;int i=0;while(i<index){curNode=curNode.next;i++;}return curNode.data;}//从链表头部删除元素public T deleteHead(){if(this.isEmpty()){return null;}Node<T> delNode=this.head.next;this.head.next=delNode.next;delNode.next=null;this.size--;return delNode.data;}//从链表尾部删除元素public T deletetail(){if (this.isEmpty()){return null;}Node<T> pre=this.head;while(pre.next.next!=null){pre=pre.next;}this.size--;Node<T> delNode=pre.next;pre.next=delNode.next;delNode.next=null;return delNode.data;}//在链表任意位置删除public T deleteByIndex(int index){int count=1;if(index<0||index>this.size){return null;}Node<T> pre=this.head;while(pre.next!=null) {pre = pre.next;count++;if (count == index) {break;}}Node<T> delNode=pre.next;pre.next=delNode.next;delNode.next=null;return delNode.data;}//根据值在链表中删除元素public int removeByValue(T value){int count=0;//定义前驱结点Node<T> pre=this.head;while(pre.next!=null){Node<T> curNode=pre.next;if(curNode.data.equals(value)){pre.next=pre.next.next;curNode.data=null;this.size--;count++;}else{pre=pre.next;}}return count;}
}

链表实现的栈的代码

public class LinkedStack<T> implements Mystack{private MyLinkList<T> data;public LinkedStack(){this.data=new MyLinkList<>();}@Overridepublic void push(Object value) {this.data.addTail((T) value);}@Overridepublic T pop() {return this.data.deleteHead();}@Overridepublic Object peek() {return this.data.getByIndex(data.getsize());}@Overridepublic boolean IsEmpty() {return this.data.isEmpty();}
}

栈的接口

public interface Mystack <T>{void push(T value);T pop();T peek();boolean IsEmpty();}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/641382.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

docker清理垃圾命令

如果使用 docker 进行大规模开发&#xff0c;但是没有清理策略&#xff0c;那么的磁盘马上就会被填满&#xff0c;进而会影响整个ci/cd的流程。 docker积累的垃圾&#xff0c;包括&#xff1a; 已经停止的容器远古镜像磁盘卷默认网络 清理停止的容器 docker rm -v $(docker…

中美比特币储备曝光

作者&#xff1a;秦晋 自1月10日&#xff0c;美国SEC审批通过包含贝莱德在内的11只比特币现货ETF之后&#xff0c;短时间内&#xff0c;其总交易量已超过130亿美元。其资产管理规模超过白银ETF&#xff0c;成为美国第二大ETF商品类别。 据《Bitcoin Magazine》报道&#xff0c;…

分享一个“产业级,开箱即用”的NLP自然语言处理工具

NLP的全称是Natuarl Language Processing&#xff0c;中文意思是自然语言处理&#xff0c;是人工智能领域的一个重要方向 自然语言处理&#xff08;NLP&#xff09;的一个最伟大的方面是跨越多个领域的计算研究&#xff0c;从人工智能到计算语言学的多个计算研究领域都在研究计…

JAVASE进阶(设计模式、设计原则)(更新中...)

目录 一、注解 内置注解&#xff1a;JAVA中已经定义好的注解。 元注解&#xff1a;修饰注解的注解。 自定义注解。 二、克隆 JAVA中对clone的实现&#xff1f; 浅克隆 深克隆 那么该如何做到深克隆呢&#xff1f; 三、常用设计模式 1、创建型模式 单例模式 工厂模式 工…

【GitHub项目推荐--一个语音机器人项目】【转载】

推荐一个腾讯大佬开源的语音对话机器人&#xff1a;wukong-robot &#xff0c;悟空机器人在 GitHub 上斩获 3.2K 的 Star。 这是一个简单灵活的中文语音对话机器人项目&#xff0c;目的是让中国的开发者也能快速打造个性化的智能音箱&#xff0c;同时&#xff0c;该项目还是第…

网络安全进阶试题——附答案

选择题 以下哪个是网络安全的首要原则&#xff1f; A. 开放访问B. 机密性C. 全球畅通D. 数据泄露 什么是“零信任”安全模型的核心概念&#xff1f; A. 信任所有用户B. 信任无线网络C. 从内到外建立信任D. 不信任任何用户&#xff0c;无论其在内部还是外部 常见的社交工程攻击手…

QTableWidget 双击单元格修改数据

本章介绍通过双击单元格&#xff0c;进入单元格&#xff0c;进行编辑&#xff0c;并对比是否修改了数据&#xff0c;如果修改了更新到数据库。 其他关于QTableWidget的操作&#xff0c;请查看上一篇文章《QTableWidget 用法-CSDN博客》 修改单元格内容&#xff0c;与原值比较…

vue3 npm i 一直卡到不动

一. 首先node 版本要18.0及以上 查看node版本并安装指定版本 二. 查看npm镜像源以及指定安装npm的镜像 三. 删除项目中的package-lock.json文件 最好是把node_modules安装包也删除掉&#xff0c;然后npm i 就可以了

2024年开启重学前端和深入浅出系列

我回来了&#xff0c;2024年决定开启前端重学系列和深入浅出系列 系统化输出文章&#xff0c;后期出书或掘金小册子 2024年决定输出&#xff1a; 重学CSS3系列 重学JS系列 重学ES6系列 深入浅出Vue3 深入浅出微前端&#xff08;qiankun、wujie&#xff09; Vue3源码分析…

(十三)Head first design patterns原型模式(c++)

原型模式 原型模式就是就是对对象的克隆。有一些私有变量外界难以访问&#xff0c;而原型模式可以做到对原型一比一的复刻。 其关键代码为下面的clone方法。此方法将本对象进行复制传递出去。 class ConcretePrototype1 : public Prototype{ public:ConcretePrototype1(stri…

配置jdk环境变量

查看jdk在什么地方的命令 echo $JAVA_HOME 查看jdk的执行路径--使用which java whereis java zip -qr jdk21.zip jdk21/ 手动配置Java环境变量 修改配置环境 vim /etc/profile 把下面的配置放到文件的最后一行 export JAVA_HOME/usr/local/jdk1.8.0_202 # 这里设置…

网络安全人员一定要知道的Metasploit渗透框架!

简介 Metasploit是一款开源安全漏洞检测工具&#xff0c;附带数百个已知的软件漏洞&#xff0c;并保持频繁更新。被安全社区冠以“可以黑掉整个宇宙”之名的强大渗透测试框架。 Metasploit官网&#xff1a;www.metasploit.com/ Metasploit的Github仓库地址&#xff1a;githu…

ERC20 解读

1.ERC20 什么叫做代币&#xff1f; 代币可以在以太坊中表示任何东西&#xff1a; 在线平台中的信誉积分游戏中一个角色的技能彩票卷金融资产类似于公司股份的资产像美元一样的法定货币一盎司黄金及更多... 以太坊的这种强大特点必须以强有力的标准来处理&#xff0c;对吗&a…

X射线中关于高频高压发生器、高清晰平板探测器、大热容量X射线球管、远程遥控系统的解释

高频高压发生器&#xff08;High Frequency High Voltage Generator&#xff09; 在医用诊断X射线设备中扮演着关键角色&#xff0c;它主要用于产生并控制用于X射线成像的高压电能。 这种发生器采用高频逆变技术&#xff0c;通过将输入的低电压、大电流转换为高电压、小电流&am…

时间序列预测 — CNN-LSTM-Attention实现多变量负荷预测(Tensorflow):多变量滚动

专栏链接&#xff1a;https://blog.csdn.net/qq_41921826/category_12495091.html 专栏内容 ​ 所有文章提供源代码、数据集、效果可视化 ​ 文章多次上领域内容榜、每日必看榜单、全站综合热榜 ​ ​ ​ ​ ​ ​ ​ 时间序列预测存在的问题 ​ 现有的大量方法没有真正的预测未…

【目标检测】YOLOv7算法实现(二):正样本匹配(SimOTA)与损失计算

本系列文章记录本人硕士阶段YOLO系列目标检测算法自学及其代码实现的过程。其中算法具体实现借鉴于ultralytics YOLO源码Github&#xff0c;删减了源码中部分内容&#xff0c;满足个人科研需求。   本篇文章在YOLOv5算法实现的基础上&#xff0c;进一步完成YOLOv7算法的实现。…

40尺货柜可以装载多少张建筑模板?

在建筑行业&#xff0c;40尺货柜一直以其标准化、便捷的特点成为建材运输的首选。然而&#xff0c;对于建筑模板这样的大型且薄型货物&#xff0c;如何在40尺货柜中实现最高效的装载一直是一项具有挑战性的任务。让我们一起揭秘&#xff0c;40尺货柜究竟能够装载多少张建筑模板…

笨蛋学设计模式行为型模式-命令模式【19】

行为型模式-命令模式 8.6命令模式8.6.1概念8.6.2场景8.6.3优势 / 劣势8.6.4命令模式可分为8.6.5命令模式8.6.6实战8.6.6.1题目描述8.6.6.2输入描述8.6.6.3输出描述8.6.6.4代码 8.6.7总结 8.6命令模式 8.6.1概念 ​ 命令模式允许将请求封装成一个对象(命令对象&#xff0c;包含…

上门回收小程序,打造回收新模式

近年来&#xff0c;我国一直秉持着环保绿色的发展理念&#xff0c;为了减少资源浪费&#xff0c;旧物回收成为了人们处理废弃物品的方式。目前&#xff0c;我国回收市场规模大约能达到3.58亿元&#xff0c;在我国经济的稳定增长和环保意识的提高下&#xff0c;回收市场规模还将…

Spring事件发布ApplicationEventPublisher原理

借鉴&#xff1a;https://blog.csdn.net/zxfhahaha/article/details/112558429 https://blog.csdn.net/qq_46940224/article/details/131043987 https://blog.csdn.net/weixin_37672801/article/details/128803430 https://www.cnblogs.com/shamo89/p/15715091.html https://bl…