加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 安全 > 正文

算法系列15天速成 第九天 队列

发布时间:2020-03-14 19:10:13 所属栏目:安全 来源:站长网
导读:可能大家都知道,线性表的变种非常非常多,比如今天讲的“队列”,灰常有意思啊

#region 获取队头元素
        /// <summary>
/// 获取队头元素
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
/// <returns></returns>
        public T SeqQueuePeek<T>(SeqQueue<T> seqQueue)
        {
            if (SeqQueueIsEmpty(seqQueue))
                throw new Exception("队列已空,不能进行出队操作");

return seqQueue.data[seqQueue.head];
        }
        #endregion

/// <summary>
/// 获取队列长度
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
/// <returns></returns>
        public int SeqQueueLen<T>(SeqQueue<T> seqQueue)
        {
            return seqQueue.tail - seqQueue.head;
        }
    }
    #endregion
}

三:顺序队列的缺陷

大家看这张图,不知道可有什么异样的感觉,在这种状态下,我入队操作,发现程序提示队列

已满,但是tnd我这个数组还有一个空间啊,是的,这就是所谓的“假溢出”。

四:循环队列
 

俗话说的好啊,“没有跨不过的坎”。

1: 概念

之所以叫“循环”,得益于神奇的“%”。他让队列的首位进行相连,形成了一个我们思维中的

“圈圈”。
 

2:循环公式

tail=(tail+1)%array.Length;

多看几眼,大家就看通了其中循环的道理,我要做成如下的图:

3:对循环的改造

先前看了一些资料,有的压根就是错的,有的说想要循环,就要牺牲一个单位的空间。

我觉得没必要。我既要循环又不牺牲空间,所以反射了一下framework中的Queue类。

改造后代码如下:

复制代码 代码如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeqQueue
{
    public class Program
    {
        static void Main(string[] args)
        {
            SeqQueue<Student> seqQueue = new SeqQueue<Student>();

SeqQueueClass queueManage = new SeqQueueClass();

Console.WriteLine("目前队列是否为空:" + queueManage.SeqQueueIsEmpty(seqQueue) + "n");

Console.WriteLine("将ID=1,2,3的实体加入队列n");
            queueManage.SeqQueueIn(seqQueue, new Student() { ID = 1, Name = "hxc520", Age = 23 });
            queueManage.SeqQueueIn(seqQueue, new Student() { ID = 2, Name = "一线码农", Age = 23 });
            queueManage.SeqQueueIn(seqQueue, new Student() { ID = 3, Name = "51cto", Age = 23 });

Console.WriteLine("n当前队列个数:" + queueManage.SeqQueueLen(seqQueue) + "");

Console.WriteLine("n*********************************************n");

Console.WriteLine("我要出队了n");
            queueManage.SeqQueueOut(seqQueue);

Console.WriteLine("哈哈,看看跟顺序队列异样之处,我再入队,看是否溢出n");
            queueManage.SeqQueueIn(seqQueue, new Student() { ID = 4, Name = "博客园", Age = 23 });
            Console.WriteLine("n....一切正常,入队成功");

Console.WriteLine("n当前队列个数:" + queueManage.SeqQueueLen(seqQueue) + "");

Console.Read();
        }
    }

#region 学生数据实体
    /// <summary>
/// 学生数据实体
/// </summary>
    public class Student
    {
        public int ID { get; set; }

public string Name { get; set; }

public int Age { get; set; }
    }
    #endregion

#region 队列的数据结构
    /// <summary>
/// 队列的数据结构
/// </summary>
/// <typeparam></typeparam>
    public class SeqQueue<T>
    {
        private const int maxSize = 3;

public int MaxSize
        {
            get { return maxSize; }
        }

/// <summary>
/// 顺序队列的存储长度
/// </summary>
        public T[] data = new T[maxSize];

//头指针
        public int head;

//尾指针
        public int tail;

//队列中有效的数字个数
        public int size;
    }
    #endregion

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读