zoukankan      html  css  js  c++  java
  • 环形队列的应用

    /// <summary>
        /// 环形队列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class CircleQueue<T>
        {
            private T[] queue;
            private int length;
            private int capacity;
            /// <summary>
            /// 头部
            /// </summary>
            private int head = 0;
            /// <summary>
            /// 尾部
            /// </summary>
            private int tail = 0;
    
            public CircleQueue(int capacity)
            {
                this.capacity = capacity;
                this.length = 0;
                this.head = 0;
                this.tail = 0;
                this.queue = new T[capacity];
            }
            /// <summary>
            /// 清空
            /// </summary>
            public void Clear()
            {
                head = 0;
                tail = 0;
                length = 0;
                this.queue = new T[this.capacity];
            }
            /// <summary>
            /// 队列是否为空
            /// </summary>
            /// <returns></returns>
            public bool IsEmpty()
            {
                return length == 0;
            }
            /// <summary>
            /// 队列是否已满
            /// </summary>
            /// <returns></returns>
            public bool IsFull()
            {
                return length == capacity;
            }
            /// <summary>
            /// 队列长度
            /// </summary>
            /// <returns></returns>
            public int Length()
            {
                return length;
            }
            /// <summary>
            /// 获取队列
            /// </summary>
            /// <returns></returns>
            public T[] GetAllQueue()
            {
                return queue;
            }
            /// <summary>
            /// 插入队列
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            public bool EnQueue(T node)
            {
                if (IsFull() == false)
                {
                    queue[tail] = node;
                    tail = (++tail) % capacity;
                    length++;
                    return true;
                }
                return false;
            }
            /// <summary>
            /// 获取队列第一个元素
            /// </summary>
            /// <returns></returns>
            public T DeQueue()
            {
                T node = default(T);
                if (IsEmpty() == false)
                {
                    node = queue[head];
                    head = (++head) % capacity;
                    length--;
                }
                return node;
            }
            /// <summary>
            /// 显示剩余数据
            /// </summary>
            public void ShowItems()
            {
                for (int i = head; i < length + head; i++)
                {
                    Console.WriteLine(queue[i % capacity]);
                }
            }
        }//end

    环形队列主要应用在单线程读或者写,无需要锁,相比较Queue队列,Queue队列可能会出现单个元素的时候又读又写造成死锁。

    欢迎指正:haizi2014@qq.com
  • 相关阅读:
    C语言实现五子棋简单功能
    C语言学习笔记——堆和栈——未整理
    《C和指针》章节后编程练习解答参考——第10章
    华为06年面试题——求交换数组元素后差值最小方案
    丢沙包游戏(或杀人游戏)的C语言实现
    《C和指针》章节后编程练习解答参考——第9章
    PHP函数:array_key_exists
    PHP函数:php_sapi_name
    PHP函数:json_last_error
    PHP函数:func_num_args
  • 原文地址:https://www.cnblogs.com/hcfan/p/7894891.html
Copyright © 2011-2022 走看看