zoukankan      html  css  js  c++  java
  • 数据结构-队列(1)

     先入先出的数据结构

    在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素

    如上图所示,队列是典型的 FIFO 数据结构。插入(insert)操作也称作入队(enqueue),新元素始终被添加在队列的末尾。 删除(delete)操作也被称为出队(dequeue)。 你只能移除第一个元素

    示例 - 队列


    1. 入队:您可以单击下面的 Enqueue 以查看如何将新元素 6 添加到队列中。

        

    2. 出队:您可以单击下面的 Dequeue 以查看将删除哪个元素。

       

    队列 - 实现

    为了实现队列,我们可以使用动态数组和指向队列头部的索引。

    如上所述,队列应支持两种操作:入队和出队。入队会向队列追加一个新元素,而出队会删除第一个元素。 所以我们需要一个索引来指出起点。

    这是一个供你参考的实现:

     1 package queue;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 //"static void main" must be defined in a public class.
     7 //“静态void main”必须在公共类中定义。
     8 class MyQueue {
     9     // store elements
    10     // 存储元素
    11     private List<Integer> data;
    12     // a pointer to indicate the start position
    13     //指示开始位置的指针
    14     private int p_start;
    15 
    16     public MyQueue() {
    17         data = new ArrayList<Integer>();
    18         p_start = 0;
    19     }
    20 
    21     /**
    22      * Insert an element into the queue. Return true if the operation is successful.
    23      */
    24     //在队列中插入一个元素。如果操作成功,返回true。
    25     public boolean enQueue(int x) {
    26         data.add(x);
    27         return true;
    28     };
    29 
    30     /**
    31      * Delete an element from the queue. Return true if the operation is successful.
    32      */
    33     //从队列中删除一个元素。如果操作成功,返回true。
    34     public boolean deQueue() {
    35         if (isEmpty() == true) {
    36             return false;
    37         }
    38         p_start++;
    39         return true;
    40     }
    41 
    42     /** Get the front item from the queue. */
    43     //从队列中获取前面的项。
    44     public int Front() {
    45         return data.get(p_start);
    46     }
    47 
    48     /** Checks whether the queue is empty or not. */
    49     //检查队列是否为空。
    50     public boolean isEmpty() {
    51         return p_start >= data.size();
    52     }
    53 };
    54 
    55 public class Main {
    56     public static void main(String[] args) {
    57         MyQueue q = new MyQueue();
    58         q.enQueue(5);
    59         q.enQueue(3);
    60         if (q.isEmpty() == false) {
    61             System.out.println(q.Front());
    62         }
    63         q.deQueue();
    64         if (q.isEmpty() == false) {
    65             System.out.println(q.Front());
    66         }
    67         q.deQueue();
    68         if (q.isEmpty() == false) {
    69             System.out.println(q.Front());
    70         }
    71     }
    72 }

    缺点


    上面的实现很简单,但在某些情况下效率很低。 随着起始指针的移动,浪费了越来越多的空间。 当我们有空间限制时,这将是难以接受的。

    让我们考虑一种情况,即我们只能分配一个最大长度为 5 的数组。当我们只添加少于 5 个元素时,我们的解决方案很有效。 例如,如果我们只调用入队函数四次后还想要将元素 10 入队,那么我们可以成功。

    但是我们不能接受更多的入队请求,这是合理的,因为现在队列已经满了。但是如果我们将一个元素出队呢?


    实际上,在这种情况下,我们应该能够再接受一个元素。

  • 相关阅读:
    内置对象 和 DropDownList时间年月日的三级联动
    复合控件 ispostback 跨页面传值
    webform简单控件和Repeater的使用
    初步认识ASP.NET WebForm
    WinForm Timer控件,三级联动[省,市,区]
    进程 线程 用户控件
    窗体容器MDI
    对话框控件 MessageBox.Show()用法补充 打开新窗体的3中模式
    winform窗体移动和阴影API
    PS学习笔记
  • 原文地址:https://www.cnblogs.com/zsh-blogs/p/9974752.html
Copyright © 2011-2022 走看看