One Pager Cheat Sheet
In this lesson, we'll cover the concept ofAbstract Data Types (ADTs), and focus on learning aboutstacksand its time complexities, as well as how to use them in programming interviews.- A Stack is an
ADTthat 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 beaddedorremovedfrom thetopso that the most recent element is always the first one to beremoved. - A
Stackis 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. 10gotpushedto the bottom and20has now beenpushedonto the stack, making20the first and last element.- When the
stack.pop()method is used, the last inserted element (in this case20) is removed, resulting in10becoming the sole first element. - In a
stack, you can onlypopelements 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
Stackclass so that it is not in anoverfloworunderflowstate, although the default inC++is usually sufficiently large. Stacksare commonly used for browser navigation, Windows operations, expression notation, syntax parsing, backtracking, memory monitoring, string reversing and a popular graph algorithm,DFS.- The
pushandpopoperations are of constant time complexityO(1), while the space complexity isO(n). - We can implement a
Stackby initializing an array of sizeMAXSIZEforstack[]and using methods such aspush(),pop(),peek(),getSize()andprint(). - The
topof a stack with aMAXSIZEof9is 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 structureconsists of important functions such as pop and push, followed by operations such aspush,pop,peek,getSize, etc. that are all called within themainfunction. - We can implement a stack using
Nodesof aLinked Listin multiple programming languages. - We can
create aNodeclassand then use it to start setting up ourStackLLclass with thetopnode initialized asnull. - When
topis notnull,pushadds a new element to the stack by makingtoppoint to a newNodecontaining the item. - We inserted our item into a
Node, creating a reference totopas thenext Nodeand assigningtopto this new node. - We remove an item from the stack by changing the
referencenodefrom the current top node to the next one, i.e.top = top.next. - The
top()function is used to check thetop.datavalue in a node; the isEmpty() method then comparestopto null to check if the stack has a value or is empty. - Stacks are
Abstract Data Typeswhich remain unchanged regardless of the underlyingdata structureand programming language used to implement them.


