码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构之队列

时间:2018-09-26 19:05:45      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:分享   front   private   代码   list   指针   设置   技术   集合   

  数据结构中队列是一种线性的存储结构,该结构的特性是先进先出(将首先处理添加到队列的第一个元素),操作步骤如下图所示:技术分享图片

  该结构的具体实现是:初始化队列,提供一个list集合data,并为该集合设置一个p_start属性,该属性记录元素头节点的位置,在添加元素时,调用add()方法,在删除元素时首先判断元素是否为空,方法位如果p_start的值大于等于data的长度则该队列为空,如果非空,将p_start的值加一。具体代码实现为:

 1 class MyQueue {
 2     private List<Integer> data;
 3     
 4     private int p_start;
 5     
 6     //构造方法,对属性进行初始化
 7     public MyQueue(){
 8         data = new ArrayList<Integer>();
 9         p_start = 0;
10     }
11     
12     public boolean enQueue(Integer num){
13         data.add(num);
14         return true;
15     }
16     
17     public boolean deQueue(){
18         if(data.isEmpty() == true){
19             return false;            
20         }
21         p_start++;
22         return true;
23     }
24     
25     public int Front(){
26         return data.get(p_start);
27     }
28     
29     public boolean isEmpty(){
30         return p_start >= data.size();
31     }
32 }

但是这种方法会造成空间的浪费, 随着起始指针的移动,浪费了越来越多的空间。 当我们有空间限制时,这将是难以接受的。

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

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

在这里,我们引入了循环队列的概念。

class MyCircularQueue {
    
    private int[] data;
    private int head;
    private int tail;
    private int size;

    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        data = new int[k];
        head = -1;
        tail = -1;
        size = k;
    }
    
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if (isFull() == true) {
            return false;
        }
        if (isEmpty() == true) {
            head = 0;
        }
        tail = (tail + 1) % size;
        data[tail] = value;
        return true;
    }
    
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if (isEmpty() == true) {
            return false;
        }
        if (head == tail) {
            head = -1;
            tail = -1;
            return true;
        }
        head = (head + 1) % size;
        return true;
    }
    
    /** Get the front item from the queue. */
    public int Front() {
        if (isEmpty() == true) {
            return -1;
        }
        return data[head];
    }
    
    /** Get the last item from the queue. */
    public int Rear() {
        if (isEmpty() == true) {
            return -1;
        }
        return data[tail];
    }
    
    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        return head == -1;
    }
    
    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        return ((tail + 1) % size) == head;
    }
}

 

数据结构之队列

标签:分享   front   private   代码   list   指针   设置   技术   集合   

原文地址:https://www.cnblogs.com/whpl22-Blog/p/9708752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!