使用C#实现顺序队列

队列(Queue)是插入操作限定在表的尾部而其它操作限定在表的头部进行的线性表。把进行插入操作的表尾称为队尾(Rear),把进行其它操作的头部称为队头(Front)。当对列中没有数据元素时称为空对列(Empty Queue)。

队列通常记为:Q= (a1,a2,…,an),a1为队头元素,an为队尾元素。元素按照a1,a2,…,an的次序依次入队,出队的次序与入队相同,即a1第一个出队,an最后一个出队。所以,对列的操作是按照先进先出(First In First Out)或后进后出( Last In Last Out)的原则进行的,因此,队列又称为FIFO表或LILO表。

队列的常用操作有:

1、构造一个空队列:InitQueue()//在C#中可以使用构造函数来实现

2、清空队列:ClearQueue()

3、判断队列是否为空:IsEmpty()

4、判断队列是否已满:IsFull()

5、求队列长度:QueueLength()

6、入队操作:In()

7、出队操作:Out()

8、得到队头元素:GetHead()

 下面给出一个实现顺序栈的源代码:

using System;

 

class Queue

{

    object[] data//数据元素

    int maxsize;    //最大容量

    int front;      //指向队头

    int rear;       //指向队尾

 

    //初始化队列

    public Queue(int size)

    {

        this.maxsize = size;

        data = new object[size];

        front = rear = -1;

    }

    //最大容量属性

    public int MaxSize

    {

        get

        {

            return this.maxsize;

        }

        set

        {

            this.maxsize = value;

        }

    }

    //队尾属性

    public int Rear

    {

        get

        {

            return this.rear;

        }

    }

    //队头属性

    public int Front

    {

        get

        {

            return this.front;

        }

    }

    //数据属性

    public object this[int index]

    {

        get

        {

            return data[index];

        }

    }

    //获得队列的长度

    public int GetQueueLength()

    {

        return rear-front;

    }

    //判断队列是否满

    public bool IsFull()

    {

        if(GetQueueLength() == maxsize)

            return true;

        else

            return false;

    }

    //判断队列是否为空

    public bool IsEmpty()

    {

        if(rear==front)

            return true;

        else

            return false;

    }

    //清空队列

    public void ClearQueue()

    {

        rear = front = -1;

    }

    //入队

    public void In(object e)

    {

        if(IsFull())

        {

            Console.WriteLine("队列已满!");

            return;

        }

        data[++rear]=e;

    }

    //出队

    public object Out()

    {

        if(IsEmpty())

        {

            Console.WriteLine("队列为空!");

            return null;

        }

        if(rear-front>0)

        {

            object tmp = data[++front];

            return tmp;

        }

        else

        {

            Console.WriteLine("全出队了!");

            ClearQueue();

            return null;

        }

    }

    //获得队头元素

    public object GetHead()

    {

        if(IsEmpty())

        {

            Console.WriteLine("队列为空!");

            return null;

        }

        return data[front+1];

    }

}

class Test

{

    static void Main()

    {

        Queue q = new Queue(1);

        int rdNum;

        Random rd = new Random();

        while(!q.IsFull())

        {

            rdNum = rd.Next(10,100);

            q.In(rdNum);

            Console.WriteLine("{0}入队,队列元素个数:{1}",rdNum,q.GetQueueLength());

           

        }

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

        while(!q.IsEmpty())

        {

            Console.WriteLine("{0}出队,队列元素个数:{1}",q.Out(),q.GetQueueLength());

        }      

    }

}

运行结果如下:

 使用C实现顺序队列 - KingLong - 翔宇亭—KingLongs Blog

如需转载请注明出处:翔宇亭—KingLong's Blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用协程(Coroutine)来实现排队运行的功能。协程是一种特殊的迭代器函数,它可以在执行过程中暂停并返回中间结果,然后再次从暂停的地方继续执行。 要实现协程的排队运行,可以使用yield return语句来暂停执行,并在需要时恢复执行。下面是一个简单的示例代码: ```csharp using System.Collections; using UnityEngine; public class CoroutineQueue : MonoBehaviour { private Queue coroutineQueue = new Queue(); private void Update() { if (coroutineQueue.Count > 0 && !IsInvoking("DequeueCoroutine")) { StartCoroutine("DequeueCoroutine"); } } public void EnqueueCoroutine(IEnumerator coroutine) { coroutineQueue.Enqueue(coroutine); } private IEnumerator DequeueCoroutine() { IEnumerator coroutine = (IEnumerator)coroutineQueue.Dequeue(); yield return StartCoroutine(coroutine); } } ``` 上述代码中,CoroutineQueue类维护了一个队列(coroutineQueue)用于存储需要排队运行的协程。在Update方法中,检查队列是否为空并且没有正在运行的协程,如果满足条件,则使用StartCoroutine方法启动一个新的协程来处理队列中的下一个任务。 EnqueueCoroutine方法用于向队列中添加新的协程任务。 使用示例: ```csharp CoroutineQueue coroutineQueue = new CoroutineQueue(); void Start() { // 添加协程任务到队列 coroutineQueue.EnqueueCoroutine(ExampleCoroutine1()); coroutineQueue.EnqueueCoroutine(ExampleCoroutine2()); } IEnumerator ExampleCoroutine1() { // 协程1的执行逻辑 yield return null; } IEnumerator ExampleCoroutine2() { // 协程2的执行逻辑 yield return new WaitForSeconds(2f); } ``` 在上述示例中,我们创建了一个CoroutineQueue实例,并添加了两个协程任务到队列中。这些协程将会按照添加的顺序依次执行。 请注意,以上示例是Unity引擎中使用协程的方式。如果你是在其他C#环境中使用,可能需要针对具体情况做一些修改和适配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值