CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Stack<T>
{
private class Elemento
{
T elem;
bool prioritario;
}
private List<T> stack = new List<T>();
private List<T> stackP = new List<T>();
private bool isEmpty = true;
private int count = 0;
private object top = null;
public bool IsEmpty
{ get { return isEmpty; } }
public int Count
{ get { return count; } }
private void PushAuxiliar(List<T> st, T obj)
{
st.Add(obj);
count++;
top = obj;
if (isEmpty)
isEmpty = false;
}
public void PushPrioritaire(T obj)
{
PushAuxiliar(stackP, obj);
}
public void Push(T obj)
{
PushAuxiliar(stack, obj);
}
public bool PrioritaireIsEmpty()
{
return stackP.Count == 0;
}
public T Pop()
{
if (!isEmpty)
{
List<T> st = stackP.Count == 0 ? stack : stackP;
T element = st.Last();
st.RemoveAt(st.Count - 1);
count--;
if (count == 0)
{
isEmpty = true;
top = null;
}
else {
top = stackP.Count > 0 ? stackP[stackP.Count - 1] : stack[stack.Count - 1];
}
return element;
}
else
throw new InvalidOperationException();
}
public object Peek()
{
if (top == null)
throw new InvalidOperationException();
else
return top;
}
public void Clear()
{
stack.Clear();
stackP.Clear();
top = null;
isEmpty = true;
count = 0;
}
}
public static void Main()
{
Stack<int> s = new Stack<int>();
s.Push(10);
s.PushPrioritaire(55);
s.Push(44);
s.Push(77);
Console.WriteLine("Lista vazia: " + s.IsEmpty);
Console.WriteLine("Primeiro pop acerta no prioritario " + s.Pop());
Console.WriteLine("Os outros pops acertam nos normais " + s.Pop());
Console.WriteLine(s.Pop());
}
}