class ListNode{ ?? ?public int val; ?? ?public int min; ?? ?public ListNode next;
?? ?public ListNode(int val, int min, ListNode next) { ?? ??? ?this.val = val; ?? ??? ?this.min = min; ?? ??? ?this.next = next; ?? ?} }
class MinStack { ?? ?private ListNode head; ?? ? //压栈 ?? ?public void push(int x){ ?? ??? ?if(empty()) ?? ??? ??? ?head = new ListNode(x,x,null); ?? ??? ?else ?? ??? ??? ?head = new ListNode(x, Math.min(x, head.min), head); ?? ?}
//出栈 ?? ?public void pop(){ ?? ??? ?if(empty()) ?? ??? ??? ?cout<<"this stack is empty!\n"<<endl; ?? ??? ?else ?? ??? ??? ?head =head.next; ?? ?}
//求栈顶元素 ?? ?public void top(){ ?? ??? ?if(empty()) ?? ??? ??? ?cout<<"this stack is empty!\n"<<endl; ?? ??? ?else ?? ??? ??? ?return head.val; ?? ?}
//获得最小元素 ?? ?public void getmin(){ ?? ??? ?if(empty()) ?? ??? ??? ?cout<<"this stack is empty!\n"<<endl; ?? ??? ?else ?? ??? ??? ?return head.min; ?? ?}
//判断栈是否为空 ?? ?public boolean empty(){ ?? ??? ?return head == null; ?? ?} }
|