Data structures are essential concepts in computer science that allow us to organize and manipulate data efficiently. In this topic, we will explore various data structures that are commonly used in programming to store and manage data.
An array is a data structure that stores a collection of elements of the same data type in contiguous memory locations. Each element in an array is accessed by its index.
Consider an array $A$ with elements $[5, 8, 3, 2, 7]$. To access the third element in the array, we use the index 2 (indices start from 0), so $A[2] = 3$.
A linked list is a data structure consisting of nodes where each node contains data and a reference (link) to the next node in the sequence.
Let's create a simple linked list with nodes containing values: $[3 \rightarrow 7 \rightarrow 5]$. Here, 3 is the first node, which points to 7, and 7 points to 5.
graph LR
A(3) --> B(7)
B --> C(5)
A stack is a data structure that follows the Last In, First Out (LIFO) principle, where elements are inserted and deleted from the same end, typically called the top.
Let's perform push and pop operations on a stack with elements $[2, 5, 8]$.
A queue is a data structure that follows the First In, First Out (FIFO) principle, where elements are inserted at the rear and deleted from the front.
Consider a queue with elements $[4, 6, 9]$.
A tree is a hierarchical data structure consisting of nodes connected by edges. It has a root node and child nodes.
Let's represent a simple tree with nodes:
1
/ \
2 3
/ \
4 5
A graph is a non-linear data structure consisting of nodes (vertices) and edges that connect these nodes.
Consider a graph with vertices $[A, B, C]$ and edges $[(A, B), (B, C)]$.
The difference between an array and a linked list lies in how the elements are stored in memory. An array stores elements in contiguous memory locations, allowing for fast random access, while a linked list uses nodes with references to the next node, enabling dynamic memory allocation and insertion/deletion operations.
2
/ \
1 3
\
4
A -- B -- C -- D
Create a free account to bookmark notes, download past papers, track your revision and get AI study help - free for Kenyan students.
Already have one? Log in
Join our list and we'll send fresh study notes and past papers straight to your inbox.