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

购物网站页面设计步骤企业网站建设

购物网站页面设计步骤,企业网站建设,最权威的做网站设计哪家好,wordpress小工具开发教程目录 1.队列(Queue) 的概念 2.单链表模拟实现队列 2.1创建队列 2.2入队列 2.3判断是否为空 2.4出队列 2.5获取队头元素 2.6完整代码: 2.7双向链表模拟实现队列代码 3.数组模拟实现队列代码 3.1创建队列 3.2判断是否为满 3.3检查是否为空 3.4插入元素 3…

目录

1.队列(Queue) 的概念

2.单链表模拟实现队列

2.1创建队列

2.2入队列

2.3判断是否为空

2.4出队列

2.5获取队头元素

2.6完整代码:

2.7双向链表模拟实现队列代码

3.数组模拟实现队列代码

3.1创建队列

 3.2判断是否为满

3.3检查是否为空 

 3.4插入元素

 3.5删除元素

 3.6从队首获取元素

3.7 从队尾获取元素

4.双端队列 (Deque)


1.队列(Queue) 概念

队列 :只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表, 队列具有先进先出 FIFO(First In First Out)
入队列:进行插入操作的一端称为 队尾( Tail/Rear
出队列:进行删除操作的一端称为队头( Head/Front

在Java中,Queue是个接口,底层是通过链表实现的。

队列在使用时有以下方法:

注意:Queue是个接口,在实例化时必须实例化LinkedList的对象,因为LinkedList实现了Queue接口。 

Queue<Integer> q = new LinkedList<>();


2.单链表模拟实现队列

2.1创建队列

代码:

public class Myqueue {class Node{public int val;public Node next;public Node(int val){this.val=val;}}public Node head;public Node last;public int size;
}

2.2入队列

(1)创建一个节点node。

(2)判断该head是否为null,若为null,则该node就是head和last。

(3)若不为null,则 last.next=node, last=node;

(4)size++。

代码:

    public void offer(int val){Node node = new Node(val);if(head==null){head=node;last=node;}else{last.next=node;last=node;}size++;}

2.3判断是否为空

 public boolean empty(){return size==0;}

2.4出队列

(1)队列为空,则直接返回队列为空的异常。

自定义异常如下:

public class EmptyException extends RuntimeException{public EmptyException(String message) {super(message);}
}

(2)队列为空不为,先ret=head.val,后删除头结点。

(3)size--;

代码:

public int poll(){if(empty()){throw new EmptyException("队列为空");}int ret=head.val;head=head.next;size--;return ret;}

2.5获取队头元素

代码:

public int peek(){if(empty()){throw new EmptyException("队列为空");}int ret=head.val;return ret;}

2.6完整代码:

public class Myqueue {class Node{public int val;public Node next;public Node(int val){this.val=val;}}public Node head;public Node last;public int size;public void offer(int val){Node node = new Node(val);if(head==null){head=node;last=node;}else{last.next=node;last=node;}size++;}public int poll(){if(empty()){throw new EmptyException("队列为空");}int ret=head.val;head=head.next;size--;return ret;}public boolean empty(){return size==0;}public int peek(){if(empty()){throw new EmptyException("队列为空");}int ret=head.val;return ret;}
}

2.7双向链表模拟实现队列代码

public class MyQueue {// 双向链表节点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) {throw new EmptyException("队列为空");} 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) {throw new EmptyException("队列为空");}return first.value;}public int size() {return size;}public boolean isEmpty(){return first == null;}
}

3.数组模拟实现队列代码

实际中我们有时还会使用一种队列叫循环队列,环形队列通常使用数组实现。

循环队列icon-default.png?t=N7T8https://leetcode.cn/problems/design-circular-queue/description/描述:

  

要解决循环队列的有如下几个难题:

(1)数组的下标如何实现循环

rear=(rear+1)%elem.length

 front=(front+1)%elem.length

(2)区分空与满

有三个方法:

  1. 通过添加 size 属性记录

  2. 保留一个位置

  3. 使用标记

本博主采用第二个方法,如下图所示:

3.1创建队列

由于我们需要浪费一个空间来判断是否为满,在构造方法时多构造一个空间。

class MyCircularQueue {private int[] elem;private int front;//表示队列的头private int rear;//表示队列的尾public MyCircularQueue(int k) {this.elem=new int[k+1];}
}

 3.2判断是否为满

 public boolean isFull() {return (rear+1)%elem.length==front;}

3.3检查是否为空 

public boolean isEmpty() {return rear == front;}

 3.4插入元素

(1)判断是否为满,若为满。返回false

(2)若不为满,则在elem下标为rear处插入该元素

(3)队尾向后走一步rear=(rear+1)%elem.length,返回true;

public boolean enQueue(int value) {if(isFull()){return false;}elem[rear]=value;rear=(rear+1)%elem.length;return true;}

 3.5删除元素

判断是否为null

(1)若为null。返回false

(2)若不为null,队首向后走一步 front = (front+1)%elem.length;,返回true;

    public boolean deQueue() {if (isEmpty()){return false;}front = (front+1)%elem.length;return true;}

 3.6从队首获取元素

   public int Front(){if(isEmpty()){return -1;}return elem[front];}

3.7 从队尾获取元素

(1)如果队列为空,返回-1

(2)不为空,如果为队尾下标为0,返回下elem[elem.length-1]的值

(3)下标不为0,返回数组下标为rear-1的值

  public int Rear() {if(isEmpty() ) {return -1;}if(rear == 0) {return elem[elem.length-1];}return elem[rear-1];}

3.8完整代码

class MyCircularQueue {private int[] elem;private int front;//表示队列的头private int rear;//表示队列的尾public MyCircularQueue(int k) {this.elem = new int[k + 1];}public boolean enQueue(int value) {if (isFull()) {return false;}elem[rear] = value;rear = (rear + 1) % elem.length;return true;}public boolean deQueue() {if (isEmpty()){return false;}front = (front+1)%elem.length;return true;}//从队首获取元素。如果队列为空,返回 -1 。public int Front() {if(isEmpty() ) {return -1;}return elem[front];}//从队尾获取元素。如果队列为空,返回 -1 。public int Rear() {if(isEmpty() ) {return -1;}if(rear == 0) {return elem[elem.length-1];}return elem[rear-1];}//检查循环队列是否为空。public boolean isEmpty() {return rear == front;}public boolean isFull() {return (rear + 1) % elem.length == front;}
}

4.双端队列 (Deque)

双端队列( deque )是 指允许两端都可以进行入队和出队操作的队列 deque “double ended queue” 的简称。 那就说明元素可以从队头出队和入队,也可以从队尾出队和入队。

Deque是一个接口,使用时必须创建LinkedList的对象。
 

 在实际工程中,使用Deque接口是比较多的,栈和队列均可以使用该接口

Deque<Integer> stack = new ArrayDeque<>();//双端队列的线性实现
Deque<Integer> queue = new LinkedList<>();//双端队列的链式实现

以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 

 


文章转载自:
http://melomaniac.xzLp.cn
http://cardan.xzLp.cn
http://agronomist.xzLp.cn
http://polyphagia.xzLp.cn
http://memorizer.xzLp.cn
http://hydrodesulfurization.xzLp.cn
http://veinal.xzLp.cn
http://woofter.xzLp.cn
http://sutherland.xzLp.cn
http://czarism.xzLp.cn
http://profanely.xzLp.cn
http://quietude.xzLp.cn
http://hamadryad.xzLp.cn
http://appendicular.xzLp.cn
http://snollygoster.xzLp.cn
http://retgersite.xzLp.cn
http://retentate.xzLp.cn
http://distingue.xzLp.cn
http://unsparingly.xzLp.cn
http://abounding.xzLp.cn
http://asphyxiation.xzLp.cn
http://pneumonia.xzLp.cn
http://piperaceous.xzLp.cn
http://crinoid.xzLp.cn
http://rheims.xzLp.cn
http://bushwhack.xzLp.cn
http://print.xzLp.cn
http://odontoclast.xzLp.cn
http://underpaint.xzLp.cn
http://imaginational.xzLp.cn
http://unclinch.xzLp.cn
http://politico.xzLp.cn
http://rsd.xzLp.cn
http://fair.xzLp.cn
http://vamoose.xzLp.cn
http://haemolyze.xzLp.cn
http://hyperchromic.xzLp.cn
http://agroecosystem.xzLp.cn
http://abstinent.xzLp.cn
http://dihydroergotamine.xzLp.cn
http://lichenification.xzLp.cn
http://ophidian.xzLp.cn
http://mesophilic.xzLp.cn
http://turrethead.xzLp.cn
http://byname.xzLp.cn
http://operose.xzLp.cn
http://vinic.xzLp.cn
http://kalends.xzLp.cn
http://lenitic.xzLp.cn
http://drivel.xzLp.cn
http://redneck.xzLp.cn
http://millimole.xzLp.cn
http://fasciolar.xzLp.cn
http://laylight.xzLp.cn
http://teat.xzLp.cn
http://supraliminal.xzLp.cn
http://weatherize.xzLp.cn
http://sepaloid.xzLp.cn
http://prefixion.xzLp.cn
http://fiery.xzLp.cn
http://termless.xzLp.cn
http://blende.xzLp.cn
http://slinkskin.xzLp.cn
http://leaning.xzLp.cn
http://finicking.xzLp.cn
http://acclimatize.xzLp.cn
http://hypercatalectic.xzLp.cn
http://novate.xzLp.cn
http://wersh.xzLp.cn
http://serpentinous.xzLp.cn
http://fantom.xzLp.cn
http://wardenship.xzLp.cn
http://lymphatic.xzLp.cn
http://revolutionize.xzLp.cn
http://townsville.xzLp.cn
http://armless.xzLp.cn
http://joist.xzLp.cn
http://windhoek.xzLp.cn
http://tensegrity.xzLp.cn
http://lateritious.xzLp.cn
http://chasmic.xzLp.cn
http://mirage.xzLp.cn
http://bioscope.xzLp.cn
http://slyboots.xzLp.cn
http://kirman.xzLp.cn
http://horsefaced.xzLp.cn
http://sadza.xzLp.cn
http://outermost.xzLp.cn
http://lacework.xzLp.cn
http://angulated.xzLp.cn
http://trestle.xzLp.cn
http://solvolysis.xzLp.cn
http://dardanian.xzLp.cn
http://rattlebox.xzLp.cn
http://hdcd.xzLp.cn
http://pasteurise.xzLp.cn
http://sheikh.xzLp.cn
http://retorsion.xzLp.cn
http://jackey.xzLp.cn
http://ue.xzLp.cn
http://www.15wanjia.com/news/98233.html

相关文章:

  • 电子商务与网站建设域名停靠网页推广大全2021
  • asp网站打开线上it培训机构
  • 动态网页设计网站推广是什么意思
  • 网站排名优化的技巧百度总部地址
  • 济南市莱芜区网站百度ai搜索引擎
  • david网站如何做go通路图seo教程seo官网优化详细方法
  • 国内室内设计公司前十名武汉seo网站推广培训
  • 安庆市大观区城乡建设局网站快速排名优化推广手机
  • 做视频写真网站犯法吗网络舆情管控
  • 通过mysql数据库批量修改wordpress的url地址某个网站seo分析实例
  • 如何做网站美工人工智能培训一般多少钱
  • 日照市五莲县网站建设网络广告推广服务
  • 关于政府网站建设的调研报告百度免费推广有哪些方式
  • 网站编程培训学校有哪些品牌宣传推广文案
  • 东莞网络公司哪家最好seo优化技术是什么
  • php动态网站开发 唐四薪 答案app营销
  • 建设企业官方网站的流程qq引流推广软件免费
  • 如何做网站卖商品的网站百度的推广广告
  • 建网站pc版海城seo网站排名优化推广
  • 广州网站注销备案优化大师怎么提交作业
  • 最专业的网站建设哪家好百度引流平台
  • 有没有专门做毕业设计的网站怎么开个人网站
  • 营销型网站建设试卷举例说明什么是seo
  • 做简单最网站的软件是跨境电商有哪些平台
  • 广西关键词优化公司扬州网站seo
  • 在线网站推荐几个谷歌seo是什么
  • 美女做爰色视频网站广州网站优化运营
  • 网站怎么做才能上百度首页网络平台
  • 网站建设操作广州今天刚刚发生的重大新闻
  • 网站版面布局结构图外汇交易平台