Beispiel #1
0
        public void Pop()
        {
            Console.WriteLine($"Going to Pop:{head.val}");

            if (min != head.val)
            {
                head      = head.prev;
                head.next = null;
            }
            else if (min == head.val)
            {
                if (head.prev == null)
                {
                    min  = 0;
                    head = null;
                }
                else
                {
                    head      = head.prev;
                    head.next = null;
                    min       = head.val;

                    while (head.prev != null)
                    {
                        if (min >= head.prev.val)
                        {
                            min = head.prev.val;
                        }
                        head = head.prev;
                    }

                    while (head.next != null)
                    {
                        head = head.next;
                    }
                }
            }

            if (head != null)
            {
                Console.WriteLine($"Top Element:{head.val}");
                count--;
            }
        }
Beispiel #2
0
 public void Push(int x)
 {
     if (head == null)
     {
         var newnode = new ListNode(x, null, null);
         head = newnode;
         min  = x;
     }
     else
     {
         var newnode = new ListNode(x, null, head);
         head.next = newnode;
         head      = head.next;
         if (min > x)
         {
             min = x;
         }
     }
     count++;
     Console.WriteLine($"Pushed :{head.val}");
 }
Beispiel #3
0
 public MinStack()
 {
     head  = null;
     min   = 0;
     count = 0;
 }