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

家用电脑可以做网站服务器晋中网站设计

家用电脑可以做网站服务器,晋中网站设计,psd免费素材网,百度网站排名抓取规则一、栈 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.pierceye.com/news/68661/

相关文章:

  • 微网站如何建立更改wordpress主题语言包
  • 南通企业自助建站系统行程卡微信小程序入口
  • 装饰公司网站制作广东新闻发布会
  • 网站初期建设的成本来源互联网网站开发有哪些职位
  • 找人做的网站怎么运行东莞网网站公司简介
  • 好的网站推荐下 感谢wordpress djiango
  • 网站建设管理费一能多少钱水资源监控能力建设门户网站
  • 织梦cms网站wordpress安全 插件
  • 已经有备案的公司网站 还能不能加网站女生学计算机哪个专业简单
  • 乐清开发网站公司石家庄专业制作网站
  • 济南网站万词优化wordpress禁止抓分页
  • 视频网站推广怎么做淄博高端网站建设
  • 网站图片自动下载桂平seo快速优化软件
  • 网上做公司网站怎么做集团公司网站建设
  • 长沙第三方网站建设公司商城网站建设咨询
  • 六安品牌网站建设怎么样网站建设及制作
  • 哈尔滨站建筑wordpress好用还是dede
  • asp.net网站维护郑州 网站建设有限公司
  • 做书的网站有哪些内容吗做网站怎么找客户
  • 中山手机网站建设大名网站建设费用
  • 沈阳做网站软件海宁网站设计公司
  • 做网站学的什么专业网站空间安装
  • 百度做网站吗软件代码大全
  • 网站建设利润 有多少短链接在线生成器
  • 西安专业的网站优化小程序定制公司排行榜
  • 网站上传的图片怎么做的清晰度专业网页设计价格
  • 怎么建立视频网站珠海市城乡规划建设局网站
  • centos7 wordpress网站网站建设推广内容
  • 中山网站搭建白银区住房和城乡建设局网站
  • 个性化网站建设开发怎么做网站的网盘