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>
|
|
|
|
|
|
public class StackQueue<T>
|
|
|
|
|
|
{
|
2021-10-29 10:14:52 +02: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-10-29 10:14:52 +02:00
|
|
|
|
public void Push(T obj) => _linkedList.AddFirst(obj);
|
|
|
|
|
|
|
|
|
|
|
|
public void Enqueue(T obj) => _linkedList.AddFirst(obj);
|
2021-09-13 21:09:47 +10:00
|
|
|
|
|
|
|
|
|
|
public T Pop()
|
|
|
|
|
|
{
|
2021-10-29 10:14:52 +02:00
|
|
|
|
T obj = _linkedList.First.Value;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
_linkedList.RemoveFirst();
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public T Dequeue()
|
|
|
|
|
|
{
|
2021-10-29 10:14:52 +02:00
|
|
|
|
T obj = _linkedList.Last.Value;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
_linkedList.RemoveLast();
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-29 10:14:52 +02:00
|
|
|
|
public T PeekStack() => _linkedList.First.Value;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
|
2021-10-29 10:14:52 +02:00
|
|
|
|
public T PeekQueue() => _linkedList.Last.Value;
|
2021-09-13 21:09:47 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|