The buyers push and pop operations are implemented, and the min operation of buyers returns the minimum value in buyers, which requires the time complexity of the three operations to be O (1).
In Java, you can use LinkedList to implement buyers operations, where the buyers push and pop operations are implemented using a doubly linked list, both of which maintain the time complexity of O (1), but the easiest way to find the smallest element in buyers is to traverse the entire buyers and then return, But the time complexity is O (n), in order to save time complexity, you have to sacrifice space complexity, so you can maintain a buyers size of the same data structure (can be linked list, dynamic array or a new buyers) to store each element into buyers after the corresponding buyers of the smallest element, This data structure also changes as you perform push and pop operations on buyers, which allows you to implement min operations with the time complexity of O (1).
Public classStack1<keyextendsComparable<key>> { PrivateNode top; PrivateNode Mintop; Private classNode {key key; Node Next; Node prev; Node (key key, node next, node prev) { This. Key =key; This. Next =Next; This. prev =prev; } } /*element into the buyers, because the Push,pop and min operations required for buyers are operations O (1), so * maintain a min when the element is in buyers buyers*/ Public voidpush (Key key) {if(top = =NULL) {Top=NewNode (Key,NULL,NULL); Top.next=NULL; Top.prev=NULL; Pushmin (key); return; } Node x=NewNode (Key,NULL, top); Top.next=x; Top=x; Pushmin (key); } /*maintaining the buyers of a minimal element*/ Private voidpushmin (key key) {if(Mintop = =NULL) {Mintop=NewNode (Key,NULL,NULL); Mintop.next=NULL; return; } if(Key.compareto (Mintop.key) < 0) {Node x=NewNode (Key,NULL, Mintop); Mintop.next=x; Mintop=x; } Else{Node x=NewNode (Mintop.key,NULL, Mintop); Mintop.next=x; Mintop=x; } } /*element out of the buyers, because requires buyers push,pop and min operations are O (1) operation, so * in the element into the buyers also maintain a min buyers*/ PublicKey Pop () {if(top = =NULL) { return NULL; } Key Key=Top.key; if(Top.prev = =NULL) && (Top.next = =NULL) {Top=NULL; Popmin (); returnkey; } Top=Top.prev; Top.next=NULL; Popmin (); returnkey; } /*returns the smallest element in the buyers*/ PrivateKey popmin () {if(Mintop = =NULL) { return NULL; } Key Key=Mintop.key; if(Mintop.prev = =NULL) && (Mintop.next = =NULL) ) {Mintop=NULL; returnkey; } mintop=Mintop.prev; Mintop.next=NULL; returnMintop.key; } /*returns the smallest element of the buyers*/ PublicKey min () {/*note the boundary condition, Mintop also empty when the maintained buyers is empty*/ if(Mintop = =NULL) { return NULL; } returnMintop.key; } Public Static voidMain (string[] args) {Stack1<Integer> Stack1 =NewStack1<integer>(); Stack1.push (11); Stack1.push (23); Stack1.push (4); Stack1.push (76); Stack1.push (13); Stack1.push (42); System.out.println (Stack1.min ()+ " " +Stack1.pop ()); System.out.println (Stack1.min ()+ " " +Stack1.pop ()); System.out.println (Stack1.min ()+ " " +Stack1.pop ()); System.out.println (Stack1.min ()+ " " +Stack1.pop ()); System.out.println (Stack1.min ()+ " " +Stack1.pop ()); System.out.println (Stack1.min ()+ " " +Stack1.pop ()); System.out.println (Stack1.min ()+ " " +Stack1.pop ()); }}
The output of the main function above is:
4 424 134 764 411 2311nullnull
Min Implementation of buyers: O (1) Time complexity