STACK

WHAT IS A STACK ??

A stack is a linear data structure that follows the LIFO (Last In First Out) principle

Last In First Out ???

Picture how pancakes are served.

After being stacked, we eat from the topmost pancake

Don't be like Dwight Schrute 😂

FIRST PANCAKE ON THE PLATE,

LAST PANCAKE TO GET OFF THE PLATE

The last element inserted is the first one to be removed

OTHER REAL LIFE EXAMPLES

Browser back button (history tracking)

Undo feature in text editors

BASIC OPERATIONS

Push ( )

Add an element to t he top  of the stack

Pop ( )

Remove the top element for the stack

Peek / Top ( )

Retrieving the top element

Size ( )

Return the number of elements in the stack

isEmpty ( )

Check if stack is empty

REPRESENTATION OF STACK

ARRAYS

Fixed size, faster access.

LINKED LIST

Dynamic size, slower access, better memory utilization

TIME COMPLEXITY

O (1)

Pop ( ), Push ( ),  Peek ( ), isEmpty ( )

SPACE COMPLEXITY

O (n)

Further Reading and Relevant Links

CODEBASICS: Stack - Data structures and Algorithm

USFCA: Stack (Array) Visualization

USFCA: Stack (Linked List) Visualization

Leetcode challenges on Stack? Here we gooooo

Thank you !!!!

STACK

By Chineletam Ugwuadu (Letam)