我想检索堆栈中输入的最小值(通过 push(int))。
这是我迄今为止编写的示例代码Push
,Pop
但我未能获得最小值
class Program
{
private int StackSize;
public int StackSizeSet
{
get { return StackSize; }
set { StackSize = value; }
}
public int top;
Object[] item;
public Program()
{
StackSizeSet = 10;
item = new Object[StackSizeSet];
top = -1;
}
public void isMinimum()
{
for (int i = 0; i <StackSizeSet; i++)
{
Console.WriteLine("Mianimum value is" + item[top]);
Console.ReadKey();
}
}
public bool isEmpty()
{
if (top == -1) return true;
return false;
}
public void Push(int value)
{
if (top == (StackSize - 1))
{
Console.WriteLine("Stack is full!");
}
else
{
item[++top] = value;
Console.WriteLine("Item pushed successfully!");
Console.WriteLine(item[top]);
}
}
public object Pop()
{
if (isEmpty())
{
Console.WriteLine("Stack is empty!");
Console.ReadLine();
return "No elements";
}
else
{
Console.WriteLine("Items popped successfully");
return item[top--];
}
}
static void Main(string[] args)
{
Program p = new Program();
p.Push(10);
p.Pop();
p.isMinimum();
//Console.WriteLine(p);
Console.ReadKey();
}
}