数据结构—栈、队列、链表

一、栈 Stack(存取O(1))

先进后出,进去123,出来321。
基于数组:最后一位为栈尾,用于取操作。
基于链表:第一位为栈尾,用于取操作。

1.1、数组栈 

/*** 基于数组实现的顺序栈; items[0]:表头/栈底;  items[size-1]:表尾/栈顶;*/
public class MyArrayStack {// 存储元素的 数组private String items[];// 栈实际大小private int size =0;// 栈的容量private int capacity = 0;public MyArrayStack(int capacity) {this.size = 0;this.capacity = capacity;this.items = new String[capacity];}/*** 入栈*/public boolean push(String item) {if(size >= capacity){throw new IndexOutOfBoundsException("MyArrayStack 栈的内存满了");}items[size] = item;size++;return true;}/*** 出栈*/public String pop() {if(size<=0){throw new IndexOutOfBoundsException("MyArrayStack 栈的内存空了");}String item = items[size-1];items[size-1] = null;size--;return item;}
}

1.2、链表栈 

/*** 基于链表实现的链式栈  top: 表尾/栈顶;*/
public class MyLinkedStack {// 表尾/栈顶;private Node top = null;/*** 入栈*/public void push(String value) {Node node = new Node(value,null);if(top != null){node.nextNode = top;}top = node;}/*** 出栈*/public String pop() {if(top==null){throw new IndexOutOfBoundsException("MyLinkedStack 栈的内存空了");}String retValue = top.getValue();top = top.nextNode;return retValue;}/*** 节点*/private static class Node{// 存储数据private String value;// 下个节点private Node nextNode;public Node(String value, Node nextNode) {this.value = value;this.nextNode = nextNode;}public String getValue() {return value;}}
}

二、队列 Queue (存取O(1))

先进先出(FIFO)的有序列表 

2.1、数组单向队列 (存取O(1))

/*** 基于数组实现的顺序队列*/
public class MyArrayQueue {private String [] items;// 容量private int capacity = 0;// 队头下标private int head = 0;// 队尾下标private int tail = 0;public MyArrayQueue(int capacity) {this.items = new String [capacity];this.capacity = capacity;}/*** 入队*/public boolean enqueue(String item) {if(capacity == tail){throw new IndexOutOfBoundsException("MyArrayQueue 容量满了");}items[tail] = item;tail++;return true;}/*** 出队*/public String dequeue() {if(head==tail){throw new IndexOutOfBoundsException("MyArrayQueue 容量空了");}String retValue = items[head];head++;return retValue;}
}

2.2、链表单向队列 

/*** 基于链表实现的链式队列*/
public class MyLinkedListQueue {// 队头private Node head = null;// 队尾private Node tail = null;/*** 入队*/public void enqueue(String value) {Node node = new Node(value,null);if(tail==null){head = node;tail = node;}else {tail.next=node;tail=node;}}/*** 出队*/public String dequeue() {if(head==null){throw new IndexOutOfBoundsException("MyLinkedListQueue 队列空了");}String retValue = head.getValue();head = head.next;if (head == null) {tail = null;}return retValue;}private static class Node{private String value;private Node next;public Node(String value, Node next) {this.value = value;this.next = next;}public String getValue() {return value;}}
}

三、链表 LinkedList 

3.1、单向链表 

/*** 单向普通链表*/
public class MyLinkedList {// 链表大小private int size;// 表头private Node head;/*** 插入*/public void insert(int index, String value) {if(index<0){throw new IndexOutOfBoundsException("MyLinkedList 下标越界了");} else {if(head==null){head = new Node(value,null);}else {if(index>=size){index = size-1;}Node prev = head;for(int i=0;i<index;i++){prev = prev.next;}Node node = new Node(value,prev);prev.next =node;}size++;}}/*** 获取*/public String get(int index){if(size<=0){throw new IllegalArgumentException("MyLinkedList 空链表");}if(index<0 || index>=size) {throw new IllegalArgumentException("MyLinkedList 下标越界了");}Node prev = head;for (int i=0;i<index;i++){prev = prev.next;}return prev.getValue();}private class Node{private String value;private Node next;public Node(String value, Node next) {this.value = value;this.next = next;}public String getValue() {return value;}}
}

3.2、双向链表 


public class MyDoubleLinkedList {// 链表大小private int size;// 表头private Node head;// 表尾private Node tail;/*** 插入*/public void insert(int index,String data) {if(index < 0) {throw new IndexOutOfBoundsException("MyDoubleLinkedList  insert 下标越界");}Node node = new Node(data,null,null);if(index == 0) {head.next = node.next;head= node;return;}if(index >= size) {tail.prev = node.prev;tail= node;return;}Node cur = this.head;while(index != 0) {cur = cur.next;index--;}node.next = cur;cur.prev.next = node;node.prev = cur.prev;cur.prev = node;}public String get(int index){if(index<0||index>size){throw new IndexOutOfBoundsException("MyDoubleLinkedList get 下标越界了");}if(index<=(size/2)){Node cur = head;for(int i = 0;i<index-1;i++){cur = head.next;}return cur.getValue();}else {index = size-index;Node cur = tail;for (int i=size;i>index;i--){cur = cur.prev;}return cur.getValue();}}private class Node{public String value;public Node prev;public Node next;public Node(String value, Node prev, Node next) {this.value = value;this.prev = prev;this.next = next;}public String getValue() {return value;}}
}

3.3、跳表 

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

(枚举 + 树上倍增)Codeforces Round 900 (Div. 3) G

Problem - G - Codeforces 题意&#xff1a; 思路&#xff1a; 首先&#xff0c;目标值和结点权值是直接联系的&#xff0c;最值不可能直接贪心&#xff0c;一定是考虑去枚举一些东西&#xff0c;依靠这种枚举可以遍历所有的有效情况&#xff0c;思考的方向一定是枚举 如果去…

协议-SSL协议-基础概念01-SSL位置-协议套件-握手和加密过程-对比ipsec

SSL的位置-思维导图 参考来源&#xff1a; 华为培训ppt:HCSCE122_SSL VPN技术 ##SSL的位置 SSL协议套件 ​​​​握手阶段&#xff0c;完成验证&#xff0c;协商出密码套件&#xff0c;进而生成对称密钥&#xff0c;用于后续的加密通信。 加密通信阶段&#xff0c;数据由对…

93、Redis 之 使用连接池管理Redis6.0以上的连接 及 消息的订阅与发布

★ 使用连接池管理Redis连接 从Redis 6.0开始&#xff0c;Redis可支持使用多线程来接收、处理客户端命令&#xff0c;因此应用程序可使用连接池来管理Redis连接。 上一章讲的是创建单个连接来操作redis数据库&#xff0c;这次使用连接池来操作redis数据库 Lettuce连接池 支持…

【maven】idea中基于maven-webapp骨架创建的web.xml问题

IDEA中基于maven-webapp骨架创建的web工程&#xff0c;默认的web.xml是这样的。 <!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name…

ADB的概念、使用场景、工作原理

文章目录 一、adb概念&#xff1a;Android Debug Bridge&#xff0c;一个可以控制安卓设备的通用命令行工具二、adb的使用场景&#xff1a;操作手机设备、app 自动化测试1.传输文件2.兼容性测试&#xff08;手机墙&#xff09;3.云测平台4.测试框架底层封装&#xff1a;APP自动…

Qt扩展-QCustomPlot绘图基础概述

QCustomPlot绘图基础概述 一、概述二、改变外观1. Graph 类型2. Axis 坐标轴3. 网格 三、案例1. 简单布局两个图2. 绘图与多个轴和更先进的样式3. 绘制日期和时间数据 四、其他Graph&#xff1a;曲线&#xff0c;条形图&#xff0c;统计框图&#xff0c;… 一、概述 本教程使用…

[C++ 网络协议] 重叠I/O模型

目录 1. 什么是重叠I/O模型 2. 重叠I/O模型的实现 2.1 创建重叠非阻塞I/O模式的套接字 2.2 执行重叠I/O的Send函数 2.3 执行重叠I/O的Recv函数 2.4 获取执行I/O重叠的函数的执行结果 2.5 重叠I/O的I/O完成确认 2.5.1 使用事件对象&#xff08;使用重叠I/O函数的第六个参…

距离矢量路由协议RIP(含Cisco模拟器实验命令配置)

距离矢量路由协议RIP(含Cisco模拟器实验命令配置) 简介 距离矢量路由协议&#xff08;Routing Information Protocol, RIP&#xff09;是一种内部网关协议&#xff0c;它位于应用层&#xff0c;使用520 UDP端口。RIP基于距离矢量算法&#xff08;Bellham-Ford&#xff09;根据…

专业图像处理软件DxO PhotoLab 7 mac中文特点和功能

DxO PhotoLab 7 mac是一款专业的图像处理软件&#xff0c;它为摄影师和摄影爱好者提供了强大而全面的照片处理和编辑功能。 DxO PhotoLab 7 mac软件特点和功能 强大的RAW和JPEG格式处理能力&#xff1a;DxO PhotoLab 7可以处理来自各种相机的RAW格式图像&#xff0c;包括佳能、…

Python3数据科学包系列(三):数据分析实战

Python3中类的高级语法及实战 Python3(基础|高级)语法实战(|多线程|多进程|线程池|进程池技术)|多线程安全问题解决方案 Python3数据科学包系列(一):数据分析实战 Python3数据科学包系列(二):数据分析实战 Python3数据科学包系列(三):数据分析实战 一: 数据分析与挖掘认知…

【大模型和智能问答系统】

大模型和智能问答系统 大模型前的智能问答系统传统管道式架构存在的问题 大模型在任务型问答系统中应用NLU应用DM如何使用大模型NLG应用 大模型前的智能问答系统 大模型统一代指以ChatGPT为代表的&#xff0c;参数量相比以前模型有明显量级变化的生成模型。 智能问答系统&…

初识Java 12-2 流

目录 中间操作 跟踪与调试 对流元素进行排序 移除元素 将函数应用于每个流元素 在应用map()期间组合流 Optional类型 便捷函数 创建Optional Optional对象上的操作 由Optional组成的流 本笔记参考自&#xff1a; 《On Java 中文版》 中间操作 ||| 中间操作&#xf…

Linux使用之xshell、xftp保姆教学(含安装包,详细使用方法,连接失败解决方法)

前言 链接: FTP&#xff0c;SSH服务器介绍 这是我之前写的一篇博客&#xff0c;其中介绍了Ubuntu操作系统的一些常用命令以及服务器介绍&#xff0c;这篇文章就向大家详细介绍如何安装及应用这些服务器&#xff0c;我以xshell、xftp为例。 安装包&#xff0c;使用方法&#xf…

华为云服务器内网vpc对等连接及微服务内网集群搭建处理

最近需要举办一场活动&#xff0c;某个业务访问量上升&#xff0c;有一定并发场景&#xff0c;为了活动能够顺利举行&#xff0c;解决方案就是将业务进行分布式&#xff0c;分布式部署到不同服务器&#xff0c;平摊用户请求&#xff0c;微服务使用的是SpringCloud Alibabanacos…

阿里云OSS图片存储

阿里云对象存储 OSS&#xff08;Object Storage Service&#xff09;是一款海量、安全、低成本、高可靠的云存储服务&#xff0c;提供最高可达 99.995 % 的服务可用性。多种存储类型供选择&#xff0c;全面优化存储成本。 视频介绍 创建bucket 开发文档 上传文件demo &#x…

树的存储结构以及树,二叉树,森林之间的转换

目录 1.双亲表示法 2.孩子链表 3.孩子兄弟表示法 4.树与二叉树的转换 &#xff08;1&#xff09;树转换为二叉树 &#xff08;2&#xff09;二叉树转换成树 5.二叉树与森林的转化 &#xff08;1&#xff09;森林转换为二叉树 以下树为例 1.双亲表示法 双亲表示法定义了…

Ai4science学习、教育和更多

11 学习、教育和更多 人工智能的进步为加速科学发现、推动创新和解决各个领域的复杂问题提供了巨大的希望。然而&#xff0c;要充分利用人工智能为科学研究带来的潜力&#xff0c;我们需要面对教育、人才培养和公众参与方面的新挑战。在本节中&#xff0c;我们首先收集了关于每…

Go-Ldap-Admin | openLDAP 同步钉钉、企业微信、飞书组织架构实践和部分小坑

目录 一、Docker-compose快速拉起demo测试环境 二、原生部署流程 安装MySQL&#xff1a;5.7数据库 安装openLDAP 修改域名&#xff0c;新增con.ldif 创建一个组织 安装OpenResty 下载后端 下载前端 部署后端 部署前端 三、管理动态字段 钉钉 企业微信 飞书 四、…

Unity自用工具:基于种子与地块概率的开放世界2D地图生成

public class BuildingGen : MonoBehaviour {public int[] Building;//存储要生成的地块代码public int[] Probability;//存储概率public double seed;public int width 100;public int height 100;public float noiseScale 0.1f; //噪声缩放倍数private int[,] frequencyM…

InnoDB索引机制

导学&#xff1a;索引什么时候失效&#xff1f;为什么类型转换索引会失效&#xff1f;不满足最左匹配原则&#xff1f; 我们都知道&#xff0c;MySQL它主要有2大模快组成&#xff0c;第一块就是我们的MySQL服务&#xff0c;里面包含了像连接管理、解析器、预处理、优化器、执行…