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

cms 美容网站 模版运城做网站设计的公司

cms 美容网站 模版,运城做网站设计的公司,容桂均安网站建设,网络广告的收费模式有哪些1. 栈(Stack) 1.1 概念 栈#xff1a;一种特殊的线性表#xff0c;其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈 顶#xff0c;另一端称为栈底。栈中的数据元素遵守后进先出LIFO#xff08;Last In First Out#xff09;的原则。 … 1. 栈(Stack) 1.1 概念 栈一种特殊的线性表其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈 顶另一端称为栈底。栈中的数据元素遵守后进先出LIFOLast In First Out的原则。 压栈栈的插入操作叫做进栈/压栈/入栈入数据在栈顶。 出栈栈的删除操作叫做出栈。出数据在栈顶。 1.2 栈的使用 从上图中可以看到Stack继承了VectorVector和ArrayList类似都是动态的顺序表不同的Vector是线程安全Vector类是线程安全的动态数组但是性能较差 , 现在已经不是很常用了 , 可以说已经过时了。 常用方法 方法功能Stack()构造一个空的栈E push(E e)将e入栈并返回eE pop()将栈顶元素出栈并返回E peek()获取栈顶元素int size()获取栈中有效元素个数boolean empty()检测栈是否为空 public static void main(String[] args) { StackInteger s new Stack(); s.push(1); s.push(2); s.push(3); s.push(4); System.out.println(s.size()); // 获取栈中有效元素个数--- 4 System.out.println(s.peek()); // 获取栈顶元素--- 4 s.pop(); // 4出栈栈中剩余1 2 3栈顶元素为3 System.out.println(s.pop()); // 3出栈栈中剩余1 2 栈顶元素为3 if(s.empty()){ System.out.println(栈空); }else{ System.out.println(s.size());} } 1.3 栈的模拟实现 从上图中可以看到Stack继承了VectorVector和ArrayList类似都是动态的顺序表不同的是Vector是线程安 全的。 代码实现 1. 构造方法 class MyStack{private int[] arr;// size 记录栈中元素个数private int size;public MyStack(){// 调用无参构造方法 默认最大容量12this(12);}public MyStack(int MaxSize){this.arr new int[MaxSize];} }2. 入栈(push) // 入栈public int push(int value){if(this.size arr.length){// 栈满 ,需要扩容int[] copyArr;// 复制arr 数组并扩容一倍copyArr Arrays.copyOf(arr,2 * arr.length);arr copyArr;}//将元素添加到size位置this.arr[size] value;// 元素个数加一this.size;// 返回添加元素return value;}3. 出栈(pop) // 出栈public int pop(){if(this.size 0){//没有元素//抛出运行时异常,此处也可以自定义异常throw new RuntimeException(栈中没有元素,不能出栈....);}// 获得栈顶元素int value this.arr[size - 1];// size - 1 之后, 下一次插入时会覆盖原数据,利用覆盖替代删除this.size--;return value;}4.获取栈顶元素(peek) // 获取栈顶元素public int peek(){if(this.size 0){//没有元素//抛出运行时异常,此处也可以自定义异常throw new RuntimeException(栈中没有元素,不能出栈....);}return this.arr[this.size - 1];}5.获取元素个数(getSize) //获取元素个数public int getSize(){return this.size;}6.判断栈是否为空(isEmpty) //判断元素是否为空public boolean isEmpty(){return this.size 0;}完整代码 import java.util.Arrays;public class MyStack{private int[] arr;// size 记录栈中元素个数private int size;public MyStack(){// 调用无参构造方法 默认最大容量12this(12);}public MyStack(int MaxSize){this.arr new int[MaxSize];}// 入栈public int push(int value){if(this.size arr.length){// 栈满 ,需要扩容int[] copyArr;// 复制arr 数组并扩容一倍copyArr Arrays.copyOf(arr,2 * arr.length);arr copyArr;}//将元素添加到size位置this.arr[size] value;// 元素个数加一this.size;// 返回添加元素return value;}// 出栈public int pop(){if(isEmpty()){//没有元素//抛出运行时异常,此处也可以自定义异常throw new RuntimeException(栈中没有元素,不能出栈....);}// 获得栈顶元素int value this.arr[size - 1];// size - 1 之后, 下一次插入时会覆盖原数据,利用覆盖替代删除this.size--;return value;}// 获取栈顶元素public int peek(){if(isEmpty()){//没有元素//抛出运行时异常,此处也可以自定义异常throw new RuntimeException(栈中没有元素,不能出栈....);}return this.arr[this.size - 1];}//获取元素个数public int getSize(){return this.size;}//判断元素是否为空public boolean isEmpty(){return this.size 0;} }1.4 栈的应用场景 1. 改变元素的序列 1. 若进栈序列为 1,2,3,4 进栈过程中可以出栈则下列不可能的一个出栈序列是      A: 1,4,3,2                      B: 2,3,4,1                 C: 3,1,4,2                            D: 3,4,2,1 根据栈先进后出的性质结合题目中进栈的过程中也可以出栈如A选项1进1出,2进3进4进4出3出2出即符合题意同理C选项1进2进3进3出之后不可能直接出1故C选项不可能实现。 2.一个栈的初始状态为空。现将元素1、2、3、4、5、A、B、C、D、E依次入栈然后再依次出栈则元素出栈的顺 序是 。 A: 12345ABCDE          B: EDCBA54321         C: ABCDE12345           D: 54321EDCBA 先进后出依次入栈依次出栈故B选项合理 2. 将递归转化为循环 递归实现逆序打印 public void display(ListNode head){if(head null){return;}//直到链表末尾再归回去if(head.next null){System.out.println(head.val );return;}display(head.next);System.out.println(head.val ); } 使用栈实现逆序打印 public void display(ListNode head){if(head null){return;}StackListNode stack new Stack();ListNode cur head;while(cur! null){stack.push(cur);cur cur.next;}while(!stack.empty()){ListNode ret stack.pop();System.out.println(ret.val );}} 2. 队列(Queue) 2.1 概念 队列只允许在一端进行插入数据操作在另一端进行删除数据操作的特殊线性表队列具有先进先出FIFO(First In First Out) 入队列进行插入操作的一端称为队尾Tail/Rear 出队列进行删除操作的一端称为队头 Head/Front 2.2 队列的使用 在Java中Queue是个接口底层是通过链表实现的。 方法功能boolean offer(E e) 入队列E poll() 出队列peek() 获取队头元素int size() 获取队列中有效元素个数boolean isEmpty()检测队列是否为空 注意Queue是个接口在实例化时必须实例化LinkedList的对象因为LinkedList实现了Queue接口。 public static void main(String[] args) { QueueInteger q new LinkedList(); q.offer(1); q.offer(2); q.offer(3); q.offer(4); q.offer(5); // 从队尾入队列 System.out.println(q.size()); System.out.println(q.peek()); // 获取队头元素 q.poll(); System.out.println(q.poll()); // 从队头出队列并将删除的元素返回 if(q.isEmpty()){ System.out.println(队列空); }else{ System.out.println(q.size()); } }2.3 队列模拟实现 队列中既然可以存储元素那底层肯定要有能够保存元素的空间通过前面线性表的学习了解到常见的空间类型有两种:顺序结构和链式结构 。 public class Queue {// 双向链表节点public static class ListNode{ListNode next;ListNode prev;int value;ListNode(int value){this.value value;}}ListNode first; // 队头ListNode last; // 队尾int size 0;// 入队列---向双向链表位置插入新节点public void offer(int e){ListNode newNode new ListNode(e);if(first null){first newNode; // last newNode;}else{last.next newNode;newNode.prev last; // last newNode;}last newNode;size;}// 出队列---将双向链表第一个节点删除掉public int poll(){ // 1. 队列为空 // 2. 队列中只有一个元素----链表中只有一个节点---直接删除 // 3. 队列中有多个元素---链表中有多个节点----将第一个节点删除int value 0;if(first null){return null;}else if(first last){last null;first null;}else{value first.value;first first.next;first.prev.next null;first.prev null;}--size;return value;}// 获取队头元素---获取链public int peek(){if(first null){return null;}return first.value;}public int size() {return size;}public boolean isEmpty(){return first null;} } 2.4 循环队列 实际中我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型时可以就会使用循环队列。环形队列通常使用数组实现。 数组下标循环的小技巧  1. 下标最后再往后(offset 小于 array.length): index (index offset) % array.length 2. 下标最前再往前(offset 小于 array.length): index (index array.length - offset)%array.length 如何区分空与满 1. 通过添加 size 属性记录 2. 保留一个位置 3. 使用标记 public class CircularQueue {private int front;private int rear;private int[] circle;public CircularQueue(int k) {//浪费掉一个存储空间circle new int[k1];}//入队列public boolean enQueue(int value) {if (isFull()) {return false;}circle[rear] value;//因为是循环队列不能写要以取模的方式rear (rear 1) % circle.length;return true;}//出队列public boolean deQueue() {if (isEmpty()) {return false;}front (front 1) % circle.length;return true;}//返回队头元素public int Front() {if (isEmpty()) {return -1;}return circle[front];}//返回队尾元素public int Rear() {if (isEmpty()) {return -1;}return circle[(rear - 1 circle.length) % circle.length];}public boolean isEmpty() {return rear front;}public boolean isFull() {return ((rear 1) % circle.length) front;}}3.  双端队列 (Deque) 双端队列deque是指允许两端都可以进行入队和出队操作的队列deque 是 “double ended queue” 的简称。那就说明元素可以从队头出队和入队也可以从队尾出队和入队。 Deque是一个接口使用时必须创建LinkedList的对象。 在实际工程中使用Deque接口是比较多的栈和队列均可以使用该接口。 DequeInteger stack new ArrayDeque();//双端队列的线性实现 DequeInteger queue new LinkedList();//双端队列的链式实现 4. 栈和队列的互相实现 用栈实现队列 class MyQueue {public StackInteger stack1;public StackInteger stack2;public MyQueue() {stack1 new Stack();stack2 new Stack();}public void push(int x) {stack1.push(x);}public int pop() {if (stack2.isEmpty()) { in2out();}return stack2.pop();}public int peek() {if (stack2.isEmpty()){ in2out();}return stack2.peek();}public boolean empty() {return stack1.isEmpty() stack2.isEmpty();}private void in2out() {while (!stack1.isEmpty()) {stack2.push(stack1.pop());}} } 用队列实现栈 class MyStack {QueueInteger queue1;QueueInteger queue2;public MyStack() {queue1 new LinkedListInteger();queue2 new LinkedListInteger();}public void push(int x) {queue2.offer(x);while (!queue1.isEmpty()) {queue2.offer(queue1.poll());}QueueInteger temp queue1;queue1 queue2;queue2 temp;}public int pop() {return queue1.poll();}public int top() {return queue1.peek();}public boolean empty() {return queue1.isEmpty();} }
http://www.zqtcl.cn/news/821059/

相关文章:

  • 商城网站建设缺点淘宝店铺怎么免费推广
  • 利于优化的网站模板360建筑网密码忘了
  • 商务网站建设找哪家网页设计商品页面制作
  • 连云港网站建设方案大型门户网站多少钱
  • win7 iis设置网站首页网站建设攵金手指科杰壹陆
  • 阿里巴巴网站建设的功能定位手机在线制作图片加字
  • 网站联系我们的地图怎么做的电子商务网站建设完整案例教程
  • 北京学习网站建设湖北省建设厅政务公开网站
  • 推广做网站联系方式贵州省领导班子名单一览表
  • 厦门的网站建设公司徐州城乡建设局网站
  • 天津圣辉友联网站建设南昌本地生活网站有哪些
  • 境外社交网站上做推广上海网站建设的价格低
  • 山西专业网站建设大全高校网站群建设研究
  • 网络营销网站建设流程网站功能设计指什么
  • 企业网络推广网站琼海市建设局网站
  • 移动网站搭建网页设计页面设计
  • 建设网站进行商品营销的重要性恢复正常百度
  • 美容会所网站模板下载jsp网站开发实现增删改查
  • 注册网站需要注意什么深圳建站公司兴田德润官网多少
  • 广东网站优化布吉做棋牌网站建设有哪些公司
  • 联邦快递的网站建设图书馆建设网站注意点
  • 西安好的皮肤管理做团购网站wordpress stats
  • 文山 网站建设 滇icp卡盟网站顶图怎么做
  • 北京网站建设公司哪些好电商建站
  • 沈阳百度广告广州营销seo
  • 营销型企业网站建设步骤做网站怎样和客户沟通
  • 多媒体教学网站开发的一般步骤网络公司网站赏析
  • 阿里云手机网站建设多少钱wordpress幻灯片制作
  • 个人博客网站下载公司邮箱免费注册
  • 厦门外贸网站建设多少钱wordpress 增大字体