[C.C++] C#集合本质之队列的用法详解

2445 0
王子 2022-11-10 08:47:56 | 显示全部楼层 |阅读模式
队列和堆栈都是约束版的链表,就像在超市购物,队列是先进先出的数据结构。
接着上一篇,派生于链表类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();
        }


以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对中国红客联盟的支持。如果你想了解更多相关内容请查看下面相关链接

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行