One Pager Cheat Sheet
In this lesson, we'll cover the concept of
Abstract Data Types (ADTs), and focus on learning about
stacksand its time complexities, as well as how to use them in programming interviews.
- A Stack is an
ADT
that allows for the addition and removal of objects in a specific order. - A stack is a data structure that follows the
Last in, First out
(LIFO) principle by requiring all items to beadded
orremoved
from thetop
so that the most recent element is always the first one to beremoved
. - A
Stack
is aLast In, First Out (LIFO)
data structure, where elements are only inserted or removed from the top, similar to "stacking" trays or plates in a cafeteria. 10
gotpushed
to the bottom and20
has now beenpushed
onto the stack, making20
the first and last element.- When the
stack.pop()
method is used, the last inserted element (in this case20
) is removed, resulting in10
becoming the sole first element. - In a
stack
, you can onlypop
elements off the top, meaning that in order to remove an element from somewhere in the middle, you must pop off all the elements above it first. - Stack overflow occurs when we try to push an element onto a stack already at
max capacity
, and stack underflow happens when we try to pop an element from an empty stack. - You need to define a size for the
Stack
class so that it is not in anoverflow
orunderflow
state, although the default inC++
is usually sufficiently large. Stacks
are commonly used for browser navigation, Windows operations, expression notation, syntax parsing, backtracking, memory monitoring, string reversing and a popular graph algorithm,DFS
.- The
push
andpop
operations are of constant time complexityO(1)
, while the space complexity isO(n)
. - We can implement a
Stack
by initializing an array of sizeMAXSIZE
forstack[]
and using methods such aspush()
,pop()
,peek()
,getSize()
andprint()
. - The
top
of a stack with aMAXSIZE
of9
is used to determine if the stack is empty or full and also indicates the position of where new elements must be stored. - A stack
data structure
consists of important functions such as pop and push, followed by operations such aspush
,pop
,peek
,getSize
, etc. that are all called within themain
function. - We can implement a stack using
Nodes
of aLinked List
in multiple programming languages. - We can
create a
Nodeclass
and then use it to start setting up ourStackLL
class with thetop
node initialized asnull
. - When
top
is notnull
,push
adds a new element to the stack by makingtop
point to a newNode
containing the item. - We inserted our item into a
Node
, creating a reference totop
as thenext Node
and assigningtop
to this new node. - We remove an item from the stack by changing the
reference
node
from the current top node to the next one, i.e.top = top.next
. - The
top()
function is used to check thetop.data
value in a node; the isEmpty() method then comparestop
to null to check if the stack has a value or is empty. - Stacks are
Abstract Data Types
which remain unchanged regardless of the underlyingdata structure
and programming language used to implement them.