is there in C# some already defined generic container which can be used as Stack and as Queue at the same time? I just want to be able to append elements either to the end,
Here's a class to help people implement this easily:
public class StackQueue
{
private LinkedList linkedList = new LinkedList();
public void Push(T obj)
{
this.linkedList.AddFirst(obj);
}
public void Enqueue(T obj)
{
this.linkedList.AddFirst(obj);
}
public T Pop()
{
var obj = this.linkedList.First.Value;
this.linkedList.RemoveFirst();
return obj;
}
public T Dequeue()
{
var obj = this.linkedList.Last.Value;
this.linkedList.RemoveLast();
return obj;
}
public T PeekStack()
{
return this.linkedList.First.Value;
}
public T PeekQueue()
{
return this.linkedList.Last.Value;
}
public int Count
{
get
{
return this.linkedList.Count;
}
}
}