2021-09-13 21:09:47 +10:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
2021-10-29 11:03:59 +02:00
|
|
|
|
namespace Umbraco.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-16 13:44:20 +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
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-16 13:44:20 +01:00
|
|
|
|
public T? PeekStack() => _linkedList.First.Value;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
|
2021-12-16 13:44:20 +01:00
|
|
|
|
public T? PeekQueue() => _linkedList.Last.Value;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|