C # stack = Read the minimum value of "1" in the stack at any time

Source: Internet
Author: User

Code essay ....

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Threading.Tasks;

Namespace Stackgetminvalues
{
public class Program
{
public void Main (string[] args)
{
Mystack mystack = new Mystack ();
Mystack.push (1);
Mystack.push (15);
Mystack.push (20);
Mystack.push (0);
Mystack.push (-10);
Mystack.push (50);
Console.WriteLine (Mystack.getminvalue ());


Mystack.pop ();
Console.WriteLine (Mystack.getminvalue ());

Mystack.pop ();
Console.WriteLine (Mystack.getminvalue ());

Console.ReadLine ();

}

}
public class Mystack
{
Public stack<int> Stackdata;
Public stack<int> stackmin;
Public Mystack ()
{
Stackdata = new stack<int> ();
Stackmin = new stack<int> ();
}
public void Push (int number)
{
if (number. GetType ()! = typeof (int)) throw new ArgumentException ("number must be entered");


if (stackdata.count () = = 0)
{
Stackdata.push (number);
Stackmin.push (number);
Return
}

Stackdata.push (number);
if (Stackmin.peek () > number)
{
Stackmin.push (number);
}
}
public int Pop ()
{
if (stackdata.count () = = 0) throw new IndexOutOfRangeException ("Stack object is empty");
var value = Stackdata.pop ();
if (value = = Stackmin.peek ())
{
Stackmin.pop ();
}
return value;
}
public int Getminvalue ()
{
if (stackmin.count () = = 0) throw new IndexOutOfRangeException ("Minimum stack object is empty");

return Stackmin.peek ();
}
}
}

C # stack = Read the minimum value of "1" in the stack at any time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.