This document introduces various data structures, including primitive structures like integers and characters, and non-primitive structures like arrays, linked lists, stacks, queues, graphs, and trees. It provides examples of how each structure organizes data and when each is useful, such as using linked lists when the data size is unknown and arrays are not flexible enough. The key advantages of linked lists over arrays for dynamic data are also summarized.
2. Spotle.ai Study Material
Spotle.ai/Learn
Data Structure is a way
of organising data so
that it can be stored,
retrieved and
manipulated easily,
efficiently and concisely.
What Does Data Structure Mean?
2
Img Src: Wikihow
3. Spotle.ai Study Material
Spotle.ai/Learn
In any large program you
will be dealing with a good
amount of data like student
records, bank transactions
and so on. How well you
organise data determines
how efficiently you:
• Add new data item
• Retrieve existing item
• Search for a data item
• Delete a data item
Why Is Data Structure Important?
3
Img Src: Youtube
4. Spotle.ai Study Material
Spotle.ai/Learn
Data Structure
Algorithm
Program
The efficiency of a computer program
depends on the efficiency of the underlying
data structure and algorithm.
4
5. Spotle.ai Study Material
Spotle.ai/Learn
Data
Structure
Primitive Data
Structure
Character Float Integer
Non-primitive
Data
Structure
Linear
Array Linked List Stack Queue
Non Linear
Graph Tree
Primitive Data Structures are the basic data structures that directly operate upon
the machine instructions. Non-primitive data structures are complex data structures
derived from the primitive data structures.
The Data Structure Hierarchy
5
6. Spotle.ai Study Material
Spotle.ai/Learn
An array is a collection of
homogeneous data elements
stored in successive
memory locations.
Good for storing unordered
lists – for example managing
a list of invitees to your
birthday party.
Arrays – The Simplest And Most Common
Non-Primitive Data Structure
List of invitees to a party stored as an array
6
Tina Didi
Guddu
Rohit
Nisha
Bobby Aunty
Chintu
Tony
7. Spotle.ai Study Material
Spotle.ai/Learn
Arrays are fixed size data
structures so you have to know
upfront how many elements you
need to work with. Say you
declared an array of size 7 and
realised you now have 10 invitees
– your program will fail at run-
time! Or you declared an array of
10 and have 7 invitees –you end
up wasting memory space.
Arrays Can Be Too Small Or Too Large For
Your Input Data Set
7
Tina Didi
Guddu
Rohit
Nisha
Bobby Aunty
Chintu
Tony
8. Spotle.ai Study Material
Spotle.ai/Learn
Inserting or deleting element from
an array is also difficult. If you
have an alphabetically sorted list
of student names as an array how
do you insert the name of a new
student? In this example to insert
Harry’s name, you have to move all
names from Mariam downwards.
Inserting Into Or Deleting From An Array Is
Difficult
8
Aman
Bairam
Charlie
Duggu
Mariam
Nisha
Tony
Harry
9. Spotle.ai Study Material
Spotle.ai/Learn
A linked list is a linear data structure where the elements are not
stored at contiguous location; the elements are linked using
pointers.
Each node of a list has 2 components - the data value and a
pointer to the next node. The last node has a pointer to null. If we
implement the alphabetically sorted list of student names as a
linked list this is what it will look like:
Linked Lists
9
Aman Bairam Charlie Duggu Mariam Nisha Tony
10. Spotle.ai Study Material
Spotle.ai/Learn
• A linked list can grow and shrink at run time so you do not have to guess the
size upfront.
• Memory is allocated at run-time so there is no memory wastage. You create
as many nodes as size of input elements at run time.
• Insertion and deletion of nodes work simply. Unlike array you don’t have to
shift elements after inserting or deleting an element. You just have to update
the pointer in the preceding node of the element being added or deleted as
you can see with the sorted student list example.
Linked Lists - Advantages
10
Aman Bairam Charlie Duggu Mariam Nisha Tony
How do you insert Harry?
Aman Bairam Charlie Duggu Harry Mariam Nisha Tony
11. Spotle.ai Study Material
Spotle.ai/Learn
Stacks
11
• A stack is a linear data
structure similar to a real stack
or pile, Like with a pile,
insertion and deletion of items
takes place at one end called
top of the stack.
• A stack is used in calling
functions, evaluating
expressions and during depth
first search.
• A stack allows two principal
operations push – to push an
element to the stack and pop
to retrieve top most element of
stack
12. Spotle.ai Study Material
Spotle.ai/Learn
Queues
12
Queues in Data Structure mimic queues in real-life. A queue is open at both
ends. You can insert data (enqueue) on one end (where the queue starts) and
remove data (dequeue) from the other end.
Queue follows First-In-First-Out methodology, that is the data item stored first will
be accessed / removed first. Queues are used by processors to schedule multiple
tasks.
13. Spotle.ai Study Material
Spotle.ai/Learn
Graphs
13
Graphs are non-linear structures that represent pair-wise relationships between
objects. A graph is made up of nodes or vertices connected by lines or edges.
Nodes: Nodes are entities whose relationships are expressed using edges.
Edges: Edges represent the relationships between nodes in a graph. An edge
between two nodes denotes a one-way or two-way relationship between the
nodes.
14. Spotle.ai Study Material
Spotle.ai/Learn
The Airlines Route Of Air India Expressed
As A Graph
14
Here the nodes denote
airports and the edges
denote routes
between two airports.
15. Spotle.ai Study Material
Spotle.ai/Learn
Trees
15
A tree data structure simulates a
hierarchical tree structure, with a root value
and subtrees of children. The first node of the
tree is called the root.
Simply put, a tree consists of nodes
connected by edges with the additional
constraint that
no reference is duplicated, and none points
to the root. The nodes with no child nodes
are called leaves.
A binary tree is a special type of tree data
structure in which each node has at most two
children.
16. Spotle.ai Study Material
Spotle.ai/Learn
Applications Of Trees
16
Binary Search Trees are used to
check whether an element is
present in a set or not.
A modified tree data structure is
used in modern routers to store
routing information.
Databases use tree structures to
hierarchically store data.
Compilers use a syntax tree to
validate the syntax of programs.
17. Spotle.ai Study Material
Spotle.ai/Learn
Understand Problem Modularize Problem Evaluate Algorithms
Evaluate Data
Structures
Choose Most Optimal
Algorithm And Data
Structure
Convert To Code
Problem Solution Approach In Computer
Science
17