C#二级数据结构实现问询:Queue为上层、LinkedList为下层
二级数据结构实现:上层Queue+下层LinkedList(.NET)
设计思路
要实现上层为Queue、下层为LinkedList的二级结构,核心逻辑如下:
- 上层使用.NET内置的
Queue<T>,严格保证元素的先进先出(FIFO)特性 - 每个队列元素是自定义类,包含键字段和LinkedList类型的信息字段,用来绑定唯一键与对应信息集合
- 下层使用
LinkedList<TInfo>存储每个键对应的信息,支持高效的头尾增删操作
之前提供的代码用Dictionary作为上层容器、Queue作为下层结构,完全不符合需求,以下是符合要求的完整实现:
完整代码实现
using System; using System.Collections.Generic; // 自定义队列元素类:绑定键与LinkedList信息集合 public class QueueElement<TKey, TInfo> { public TKey Key { get; } public LinkedList<TInfo> InfoList { get; } public QueueElement(TKey key) { Key = key; InfoList = new LinkedList<TInfo>(); } public QueueElement(TKey key, IEnumerable<TInfo> initialInfos) { Key = key; InfoList = new LinkedList<TInfo>(initialInfos); } } // 二级结构核心类:上层Queue,下层LinkedList public class TwoLevelQueueLinkedList<TKey, TInfo> { private readonly Queue<QueueElement<TKey, TInfo>> _topLevelQueue; // 初始化结构 public TwoLevelQueueLinkedList() { _topLevelQueue = new Queue<QueueElement<TKey, TInfo>>(); } // 新增顶层队列元素(可带初始信息) public void AddTopElement(TKey key, IEnumerable<TInfo> initialInfos = null) { if (ContainsKey(key)) throw new InvalidOperationException($"键 {key} 已存在"); var element = initialInfos != null ? new QueueElement<TKey, TInfo>(key, initialInfos) : new QueueElement<TKey, TInfo>(key); _topLevelQueue.Enqueue(element); } // 向指定键的下层LinkedList添加信息 public void AddInfoToKey(TKey key, TInfo info) { var targetElement = FindElementByKey(key); if (targetElement == null) throw new KeyNotFoundException($"未找到键 {key}"); targetElement.InfoList.AddLast(info); } // 删除顶层队列的首元素(遵循FIFO) public bool RemoveTopElement() { if (IsEmpty()) return false; _topLevelQueue.Dequeue(); return true; } // 删除指定键对应的顶层元素及所有下层信息 public bool RemoveElementByKey(TKey key) { // Queue不支持直接删除中间元素,临时转List处理 var tempList = new List<QueueElement<TKey, TInfo>>(_topLevelQueue); var removedCount = tempList.RemoveAll(e => EqualityComparer<TKey>.Default.Equals(e.Key, key)); if (removedCount > 0) { _topLevelQueue.Clear(); foreach (var element in tempList) _topLevelQueue.Enqueue(element); } return removedCount > 0; } // 查看顶层队列的首元素(返回键和首条信息) public (TKey Key, TInfo FirstInfo) PeekTopElement() { if (IsEmpty()) throw new InvalidOperationException("结构为空"); var topElement = _topLevelQueue.Peek(); if (topElement.InfoList.Count == 0) throw new InvalidOperationException($"顶层元素 {topElement.Key} 无信息"); return (topElement.Key, topElement.InfoList.First.Value); } // 判断结构是否为空(定义为顶层队列为空则为空) public bool IsEmpty() { return _topLevelQueue.Count == 0; } // 显示整个结构的内容 public void DisplayStructure() { if (IsEmpty()) { Console.WriteLine("结构为空"); return; } Console.WriteLine("二级结构内容:"); foreach (var element in _topLevelQueue) { Console.WriteLine($"[键: {element.Key}] 信息列表:"); if (element.InfoList.Count == 0) { Console.WriteLine(" 无信息"); continue; } foreach (var info in element.InfoList) { Console.WriteLine($" - {info}"); } } Console.WriteLine(); } // 辅助方法:根据键查找对应元素 private QueueElement<TKey, TInfo> FindElementByKey(TKey key) { foreach (var element in _topLevelQueue) { if (EqualityComparer<TKey>.Default.Equals(element.Key, key)) return element; } return null; } // 检查指定键是否存在 public bool ContainsKey(TKey key) { return FindElementByKey(key) != null; } } // 使用示例 class Program { static void Main() { var structure = new TwoLevelQueueLinkedList<int, string>(); // 初始化并添加元素 structure.AddTopElement(1, new[] { "用户A的信息1", "用户A的信息2" }); structure.AddTopElement(2, new[] { "用户B的信息1" }); structure.AddTopElement(3); // 显示初始结构 Console.WriteLine("=== 初始结构 ==="); structure.DisplayStructure(); // 向下层添加信息 structure.AddInfoToKey(3, "用户C的信息1"); structure.AddInfoToKey(3, "用户C的信息2"); Console.WriteLine("=== 添加信息后 ==="); structure.DisplayStructure(); // 查看首元素 var topElement = structure.PeekTopElement(); Console.WriteLine($"顶层首元素:键={topElement.Key},首条信息={topElement.FirstInfo}\n"); // 判断是否为空 Console.WriteLine($"结构是否为空:{structure.IsEmpty()}\n"); // 删除顶层首元素 structure.RemoveTopElement(); Console.WriteLine("=== 删除顶层首元素后 ==="); structure.DisplayStructure(); // 删除指定键的元素 structure.RemoveElementByKey(2); Console.WriteLine("=== 删除键为2的元素后 ==="); structure.DisplayStructure(); } }
关键说明
- 上层Queue特性:顶层元素严格遵循FIFO规则,
RemoveTopElement()始终删除最早加入的元素 - 下层LinkedList特性:每个键对应的信息列表支持高效的头尾增删,适合频繁修改的场景
- 键唯一性:通过
ContainsKey方法检查键是否重复,避免同一键多次加入顶层队列 - 操作覆盖:完整实现了初始化、增删元素、查看首元素、判空、显示结构等所有要求的操作
内容的提问来源于stack exchange,提问作者DAS
相关产品推荐
相关产品推荐

