A Stack is one-ended linear data structure which models a real world stack by having two primary operations, namely push
and pop
.
You can think of the Stack as a container, in which we can add items and remove them. Only the top of this container is open, so the item we put in first will be taken out last, and the items we put in last will be taken out first. This is called the last in first out (LIFO) ordering.
Stacks and queues can be effectively implemented using either arrays or linked lists. The key issue is whether an upper bound on the size of the container is known in advance, thus permitting the use of a statically-allocated array.
Unlike an array, a stack does not offer constant time access to the i-th
item. However, it does allow constant-time adds and removes, ad it doesn't require shifting elements around.