Data Structures & Algorithms for Beginners - 2

Stack


A. Introduction to Stack

A stack is a one-ended linear data structure which models a real world stack by having two primary operations, namely push and pop.

image-20220519142219909

1. Instructions

image-20220519142602546

 

2. When & Where to use

 

3. Complexity

NameBig O Notation
PushingO(1)
PoppingO(1)
PeekingO(1)
SearchingO(n)
SizeO(1)

 

4. Example - Brackets

BracketsValid or Not?
[ { } ]Valid
( ( ) ( ) )Valid
{ ]Invalid
[ ( ) ] ) ) ( )Invalid
[ ] { } ( { } )Valid

 

 

B. Stack Implementation

1. Pushing

Instructions

image-20220519193353686

 

2. Popping

Instructions

image-20220519193535200

 

C. Stack Source Code