Stack is a LIFO (last-in-first-out) data structure. Which means for one element, every element added after this element must be removed before this element can be removed. (Think tissues in tissue box, we need to get the tissue on the top first).
- my implement.
- Time complexity:
pushandpopboth O(1). - We can use JavaScript array to simulate stack, use
pushto push andpopto pop. - This only simulate the 'behavior' (FIFO), JS array is in fact an object, which is just like a hashmap. The time complexity of
popandpushare both O(n), but sometimes need to resize the hashmap, which means O(n).