当前位置: 首页 > news >正文 深圳网站设计 制作元免费可商用图片素材网站 news 2025/11/4 8:17:21 深圳网站设计 制作元,免费可商用图片素材网站,天猫网站是用什么技术做的,h5自适应网站建设一、栈 Stack 1.特点 #xff08;1#xff09;栈是一种线性数据结构 #xff08;2#xff09;规定只能从栈顶添加元素#xff0c;从栈顶取出元素 #xff08;3#xff09;是一种先进后出的数据结构#xff08;Last First Out#xff09;LIFO 2.具体实现 Java中可…一、栈 Stack 1.特点 1栈是一种线性数据结构 2规定只能从栈顶添加元素从栈顶取出元素 3是一种先进后出的数据结构Last First OutLIFO 2.具体实现 Java中可以直接调用方法来实现栈 如何自己写代码来实现栈呢 先定义一个接口方便后边进行调用 package com.algo.lesson.lesson02.stack;public interface Stack_IT {//入栈void push(T ele);//出栈T pop();//查看栈顶元素T peek();//判断是否为空boolean isEmpty();//后去栈中元素int getSize(); }接下来来实现栈的方法调用接口完善方法 package com.algo.lesson.lesson02.stack;import com.algo.lesson.lesson01.MyArr;//以数组作为栈顶的底层数据结构 public class ArrStackT implements Stack_IT {private MyArrTdata;int size;public ArrStack() {this.datanew MyArr(100);this.size0;}Overridepublic void push(T ele) {//在数组尾部添加元素this.data.add(ele);this.size;}Overridepublic T pop() {if(isEmpty()){return null;}this.size--;return this.data.removeBFromLast();}Overridepublic T peek() {return this.data.getLastValue();}Overridepublic boolean isEmpty() {return this.size0;}Overridepublic int getSize() {return this.size;} }以上就是方法的代码接下来写个main函数来调用检查方法是否正确 package com.algo.lesson.lesson02.stack;import java.util.ArrayList; import java.util.List; import java.util.Random;public class StackTestT {public void test(Stack_ITstack, ListT list){//开始时间long startTimeSystem.nanoTime();for(int i0;ilist.size();i){stack.push(list.get(i));}System.out.println(stack.getSize());while(!stack.isEmpty()){T elestack.pop();System.out.println(ele );}//结束时间long endTimeSystem.nanoTime();System.out.println(总耗时(endTime-startTime)/100000000.0);}public static void main(String[] args) {StackTestIntegerstackTestnew StackTest();Stack_IIntegerstacknew ArrStack();ListIntegerlistnew ArrayList();Random randomnew Random();for(int i0;i100;i){int val random.nextInt(1000);list.add(val);}stackTest.test(stack,list);} }注其中long startTimeSystem.nanoTime();方法是获取一个时间单位毫秒 在程序运行前和运行后个添加一个最后将两个时间相减得到程序运行时间。 3.时间复杂度分析 二、队列 1.特点 1队列也是一种线性数据结构 2只能从一端添加元素另一端取出元素 3是一种先进先出的数据结构FIFO——fist in fist out 2.具体实现 Java中也可以直接调用队列的方法 自己的实现 接口 package com.algo.lesson.lesson02.queue;public interface Queue_IT {void offer(T ele);//入队T poll();//出队T peek();//查找队首元素int getSize();boolean isEmpty();}方法代码 package com.algo.lesson.lesson02.queue;import com.algo.lesson.lesson01.MyArr;public class ArrQueueTimplements Queue_IT {private MyArrTdata;/*private int size;//队列中元素的个数 */public ArrQueue(){this.datanew MyArr(50);}Overridepublic void offer(T ele) {this.data.add(ele);}Overridepublic T poll() {if(isEmpty()){return null;}return this.data.removeByIndex(0);}Overridepublic T peek() {if(isEmpty()){return null;}return this.data.getValueByIndex(0);}Overridepublic int getSize() {return this.data.getSize();}Overridepublic boolean isEmpty() {return this.data.isEmpty();} }3.出现的问题 入队列时间复杂度为O(n)因为在出队时元素要前移会出现假溢出的情况。 所以就出现了循环队列来解决这个问题 循环队列 front记录队首tail记录队尾队尾达到容积时返回到队头将空位置补上可以继续存储数据。 package com.algo.lesson.lesson02.queue;import java.util.Random;/* 基于Java中的数组进行二次封装,制作一个可变数组*/ //泛型:就是类型作为参数 public class LoopArrT {private T[] data;//保存数据private int size;//数组中实际存放元素的个数private int front;//队首private int tail;//队尾int capacity;//容积//构造函数public LoopArr(int capacity) {if (capacity 0) {this.capacity 11;} else {this.capacity capacity 1;}this.size 0;this.front this.tail 0;this.data (T[]) (new Object[this.capacity]);}//获取数组中实际存放元素的个数public int getSize() {return this.size;}//获取数组的容积public int getCapacity() {return this.capacity;}//判断数组是否为空public boolean isEmpty() {return this.front this.tail;}//向数组中添加元素(尾部)public void add(T item) {//判断数组是否满if ((this.tail 1) % this.capacity this.front) {//扩容resize((this.capacity-1)*21);}//从index位置开始元素需要进行后移this.data[this.tail] item;this.tail (this.tail 1) % this.capacity;//更新this.sizethis.size;}private void resize(int newCapacity) {System.out.println(resize: newCapacity);T[] newData (T[]) (new Object[newCapacity]);//将原数组驾到新数组里for (int i 0; i this.size; i) {newData[i] this.data[(this.fronti)%this.capacity];}//改变容器this.data newData;this.capacity newCapacity;//将this.front置零this.front0;this.tailthis.size;}//获取指定位置的值public T getValueByIndex() {if(isEmpty()){return null;}return this.data[this.front];}//移除队首元素public T remove() {if (isEmpty()) {return null;}//删除操作的核心/*1.找到删除的位置2.删除位置之后的元素要前移 arr[j-1]arr[j]*/T delValue this.data[this.front];this.front (this.front 1) % this.capacity;this.size--;//判断是否缩容if (this.size this.capacity / 4 this.capacity / 2 0) {resize((this.capacity-1) / 2 1);}return delValue;}Overridepublic String toString() {StringBuilder sb new StringBuilder([);for (int i 0; i this.size; i) {sb.append(this.data[i]);if (i ! this.size - 1) {sb.append(,);}}sb.append(]);return sb.toString();} } package com.algo.lesson.lesson02.queue;public class LoopQueueT implements Queue_IT{private LoopArrTdata;//容器public LoopQueue(){this.datanew LoopArr(100);}Overridepublic void offer(T ele) {this.data.add(ele);}Overridepublic T poll() {return this.data.remove();}Overridepublic T peek() {return this.data.getValueByIndex();}Overridepublic int getSize() {return this.data.getSize();}Overridepublic boolean isEmpty() {return this.data.isEmpty();} }4.循环队列的复杂度分析 三、栈和队列的互相实现 既然我们了解了栈和队列知道了这两种数据结构十分相似也就可以大胆假设以下可不可以相互实现不是用上面所写的以数组为底层而是相互为底层存储。 1.用栈来实现队列 import java.util.Stack;class MyQueue {private StackInteger A;private StackInteger B;public MyQueue() {A new Stack();B new Stack();}public void push(int x) {while (!B.isEmpty()) {A.push(B.pop());}A.push(x);while (!A.isEmpty()) {B.push(A.pop());}}public int pop() {return B.pop();}public int peek() {return B.peek();}public boolean empty() {return B.isEmpty();} }2.用队列实现栈 import java.util.LinkedList; import java.util.Queue;class MyStack {private QueueInteger queue1;private QueueInteger queue2;public MyStack() {queue1 new LinkedList();queue2 new LinkedList();}public void push(int x) {queue2.add(x);while (!queue1.isEmpty()) {queue2.add(queue1.remove());}QueueInteger temp queue1;queue1 queue2;queue2 temp;}public int pop() {return queue1.remove();}public int top() {return queue1.peek();}public boolean empty() {return queue1.isEmpty();} }这就是这两种数据结构相互实现的代码 在LeetCode中也有相对应的题目 力扣LeetCode官网 - 全球极客挚爱的技术成长平台栈实现队列 力扣LeetCode官网 - 全球极客挚爱的技术成长平台队列实现栈 文章转载自: http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.rbjp.cn.gov.cn.rbjp.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.lqynj.cn.gov.cn.lqynj.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.vjwkb.cn.gov.cn.vjwkb.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.irqlul.cn.gov.cn.irqlul.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.grnhb.cn.gov.cn.grnhb.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.rpkl.cn.gov.cn.rpkl.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.qjrjs.cn.gov.cn.qjrjs.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.kjgrg.cn.gov.cn.kjgrg.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.cnkrd.cn.gov.cn.cnkrd.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.zlchy.cn.gov.cn.zlchy.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.drcnf.cn.gov.cn.drcnf.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.hhxpl.cn.gov.cn.hhxpl.cn 查看全文 http://www.tj-hxxt.cn/news/276065.html 相关文章: app制作和网站一样吗美食网站建设规划书需求分析 西宁建一个网站公司佛山网站设计特色 去电商公司上班怎么样惠州百度seo哪家好 网站建设和微信小程序长春做网站公司 公司在网站做广告怎么做分录商城网站免费模板 做网站做那一网站好浙江鸿翔水利建设有限公司网站 网站后台如何用代码上传视频南京做网站费用 建设网站的准备工作网站建设合同严瑾 中国建设银行网站缴费系统西安找工作 书店网站建设方案网站建设要 网站搜索下拉是怎么做的益阳北京网站建设 做谷歌网站使用什么统计代码吗学校网站怎么查询录取 泰安钢管网站建设计算机网站建设和维护 手机算命网站建设上海注册公司免费地址 做商业网站的服务费维护费网站开发前端模板 昆明网站seo优化怎么建设域名和网站 云南网站定制开发dede网站演示 宜兴公司做网站cms系统和网站后台系统 传奇游戏网站vps转移网站 企业网站设计需求文档牡丹江做网站建设 通过php获取手机网站访客的手机号码专业做调查的网站 移动端网站怎么做优化深圳住房和建设局网站公开招标 网站手绘教程门户网站开发需要 青海 网站开发 图灵wordpress小说网自动采集 网站验收技术指标制作网站404页面 电商电商网站建设成品网站 免费 网站上有什么作用郑州网站运营 ps常用素材网站有哪些网络有限公司 在线课程软件网站建设费用昆明官方网站建设 做彩票网站犯法吗ppt素材模板免费下载