• 队列 Queue


    队列:

    1.先进先出,后进后出;

    2.支持入队Enqueue(将数据放到队尾)和出队Dequeue(取出队头数据)操作;

    3.和栈一样属于操作受限的线性表;

    如何实现队列?

    1.数组实现(顺序队列)

        public class ArrayQueue
        {
            private string[] items;
            private int capacity;
            private int head = 0;
            private int tail = 0;
    
            // 申请数组大小
            public ArrayQueue(int capacity)
            {
                items = new string[capacity];
                this.capacity = capacity;
            }
    
            public bool Enqueue(string item)
            {
                // 队列末尾没有空间
                if (tail == capacity)
                {
                    // 数组满了
                    if (head == 0) return false;
    
                    // 数据搬移
                    for (int i = head; i < tail; i++)
                    {
                        items[i - head] = items[i];
                    }
    
                    // 搬移完成重新更新tail跟head
                    tail -= head;
                    head = 0;
                }
    
                items[tail] = item;
                tail++;
                return true;
            }
    
            public string Dequeue()
            {
                // 队列为空
                if (head == tail) return null;
                string value = items[head];
                head++;
                return value;
            }
        }

    2.链表实现(链式队列)

        public class QueueLinkList<T>
        {
            private StackListNode<T> _head;
            private StackListNode<T> _tail;
            
            public void Enqueue(T val)
            {
                if(_tail==null)
                {
                    StackListNode<T> newNode = new StackListNode<T>(val);
                    _tail = newNode;
                    _head = newNode;
                }
                else
                {
                    _tail.Next = new StackListNode<T>(val);
                    _tail = _tail.Next;
                }
            }
    
            public T Dequeue()
            {
                if (_head == null) return default(T);
    
                T value = _tail.Value;
                _head = _head.Next;
    
                if (_head == null)
                    _tail = null;
    
                return value;
            }
        }
    
        public class StackListNode<T>
        {
            public StackListNode(T nodeValue)
            {
                Value = nodeValue;
            }
    
            public T Value { get; set; }
            public StackListNode<T> Next { get; set; }
        }

    3.循环队列

        // 数组实现队列会有数据搬移操作,可以通过循环队列的方式解决数据偏移,但是循环队列会浪费数组一个存储空间
        // 循环队列最关键是需要确定好队列为空以及队列满的判定条件 
    // 队满:(tail+1)%n==head 队空:tail==head
    public class CircularQueue { private string[] items; private int capacity; private int head = 0; // 列头 private int tail = 0; // 列尾 public CircularQueue(int capacity) { items = new string[capacity]; this.capacity = capacity; } public bool Enqueue(string item) { // 队列满了 if ((tail + 1) % capacity == head) return false; items[tail] = item; tail = (tail + 1) % capacity; return true; } public string Dequeue() { // 队列为空 if (tail == head) return null; string value = items[head]; head = (head + 1) % capacity; return value; } }

     

    队列的常见应用:

    1.阻塞队列
    1)在队列的基础上增加阻塞操作,就成了阻塞队列。
    2)阻塞队列就是在队列为空的时候,从队头取数据会被阻塞,因为此时还没有数据可取,直到队列中有了数据才能返回;如果队列已经满了,那么插入数据的操作就会被阻塞,直到队列中有空闲位置后再插入数据,然后在返回。
    3)从上面的定义可以看出这就是一个“生产者-消费者模型”。这种基于阻塞队列实现的“生产者-消费者模型”可以有效地协调生产和消费的速度。当“生产者”生产数据的速度过快,“消费者”来不及消费时,存储数据的队列很快就会满了,这时生产者就阻塞等待,直到“消费者”消费了数据,“生产者”才会被唤醒继续生产。不仅如此,基于阻塞队列,我们还可以通过协调“生产者”和“消费者”的个数,来提高数据处理效率,比如配置几个消费者,来应对一个生产者。


    2.并发队列
    1)在多线程的情况下,会有多个线程同时操作队列,这时就会存在线程安全问题。能够有效解决线程安全问题的队列就称为并发队列。
    2)并发队列简单的实现就是在enqueue()、dequeue()方法上加锁,但是锁粒度大并发度会比较低,同一时刻仅允许一个存或取操作。
    3)实际上,基于数组的循环队列利用CAS原子操作,可以实现非常高效的并发队列。这也是循环队列比链式队列应用更加广泛的原因。


    3.线程池资源枯竭是的处理
    在资源有限的场景,当没有空闲资源时,基本上都可以通过“队列”这种数据结构来实现请求排队。

  • 相关阅读:
    Kubernetes 升级过程记录:从 1.17.0 升级至最新版 1.20.2
    歼10:职业有个基本属性,为了糊口。然后再区分 主动 or 被动,被动者只是为了糊口,那自然是看不起自己的职业
    红胖子:写给人生的九封信,愿你的人生淡定从容,繁华似锦!!!
    Embarcadero MVP(68位,全部都有个人主页)
    Delphi 26 岁
    C# Assembly 与 Reflection
    你精通那么多技术,为何还是做不好一个项目
    Linux 6 个“吓人”的 命令
    Distributed Application runtime
    RocketMQ and kafka
  • 原文地址:https://www.cnblogs.com/zhengzc/p/11620172.html
Copyright © 2020-2023  润新知