软件编程
位置:首页>> 软件编程>> C#编程>> C#集合本质之队列的用法详解

C#集合本质之队列的用法详解

作者:Darren?Ji  发布时间:2023-03-17 06:42:38 

标签:C#,集合,本质,队列

队列和堆栈都是约束版的链表,就像在超市购物,队列是先进先出的数据结构。

接着上一篇,派生于链表类List,来模拟一个队列。

namespace LinkedListLibrary
{
   public class QueueInheritance : List
   {
       public QueueInheritance() : base("queue"){}
       //入队:到最后面
       public void Enqueue(object dataValue)
       {
           InsertAtBack(dataValue);
       }
       //出队:在最前面删除
       public object Dequeue()
       {
           return RemoveFromFront();
       }
   }
}

客户端调用。

public static void Main(string[] args)
       {
           QueueInheritance queue = new QueueInheritance();
           bool aBoolean = true;
           char aChar = 'a';
           int anInt = 1;
           string aStr = "hello";
           queue.Enqueue(aBoolean);
           queue.Display();
           queue.Enqueue(aChar);
           queue.Display();
           queue.Enqueue(anInt);
           queue.Display();
           queue.Enqueue(aStr);
           queue.Display();
           object removedObject = null;
           try
           {
               while (true)
               {
                   removedObject = queue.Dequeue();
                   Console.WriteLine(removedObject + "出队列~~");
                   queue.Display();
               }
           }
           catch (EmptyListException emptyListException)
           {
               Console.Error.WriteLine(emptyListException.StackTrace);
           }
           Console.ReadKey();
       }

C#集合本质之队列的用法详解

来源:https://www.cnblogs.com/darrenji/p/3890618.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com