2022-05-04 08:10:27 +02:00
|
|
|
|
namespace Umbraco.Cms.Core.Collections
|
2021-09-13 21:09:47 +10:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
|
/// Collection that can be both a queue and a stack.
|
2021-09-13 21:09:47 +10:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2021-12-17 16:33:23 +01:00
|
|
|
|
public class StackQueue<T>
|
2021-09-13 21:09:47 +10:00
|
|
|
|
{
|
2021-12-16 13:44:20 +01:00
|
|
|
|
private readonly LinkedList<T?> _linkedList = new ();
|
2021-09-13 21:09:47 +10:00
|
|
|
|
|
2021-10-29 10:14:52 +02:00
|
|
|
|
public int Count => _linkedList.Count;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
|
2021-10-29 10:14:52 +02:00
|
|
|
|
public void Clear() => _linkedList.Clear();
|
2021-09-13 21:09:47 +10:00
|
|
|
|
|
2021-12-16 13:44:20 +01:00
|
|
|
|
public void Push(T? obj) => _linkedList.AddFirst(obj);
|
2021-10-29 10:14:52 +02:00
|
|
|
|
|
2021-12-16 13:44:20 +01:00
|
|
|
|
public void Enqueue(T? obj) => _linkedList.AddFirst(obj);
|
2021-09-13 21:09:47 +10:00
|
|
|
|
|
|
|
|
|
|
public T Pop()
|
|
|
|
|
|
{
|
2021-12-16 13:44:20 +01:00
|
|
|
|
T? obj = default(T);
|
|
|
|
|
|
if (_linkedList.First is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
obj = _linkedList.First.Value;
|
|
|
|
|
|
}
|
2021-09-13 21:09:47 +10:00
|
|
|
|
_linkedList.RemoveFirst();
|
2021-12-16 13:44:20 +01:00
|
|
|
|
return obj!;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public T Dequeue()
|
|
|
|
|
|
{
|
2021-12-16 13:44:20 +01:00
|
|
|
|
T? obj = default(T);
|
|
|
|
|
|
if (_linkedList.Last is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
obj = _linkedList.Last.Value;
|
|
|
|
|
|
}
|
2021-09-13 21:09:47 +10:00
|
|
|
|
_linkedList.RemoveLast();
|
2021-12-16 13:44:20 +01:00
|
|
|
|
return obj!;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-13 09:27:37 +01:00
|
|
|
|
public T? PeekStack() => _linkedList.First is not null ? _linkedList.First.Value : default;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
|
2022-01-13 09:27:37 +01:00
|
|
|
|
public T? PeekQueue() => _linkedList.Last is not null ? _linkedList.Last.Value : default;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|